Reply to topic  [ 7 posts ] 
Finding files with a batch file. 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Hi :D

Now, I know a bit about writing batch files and scripts etc... but this is a bit beyond my knowledge.

The software we develop at work has 2 sorts of files. Binary and source (as per any programming language).

The source files end in .p or .w and the binaries end in .r

Now we sometimes get problems where someone has edited a source file on their PC but there is a rogue binary file in someone else's network folder that is taking priority and being run instead of the new one.

WHat I would like to do is write a batch file to go through these user network folder and find each .r file and then see if there is a .w or .p in the same folder. If there is NOT a .p or .w in the same folder then delete the .r file.

I imagine this is possible (I could probably do something in Java but it would a lot more complex than I imagine it would be in a batch file).

Thanks for any help :D

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed May 06, 2009 9:20 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:58 pm
Posts: 8767
Location: behind the sofa
Reply with quote
Personally, I'd dump the directory listing into an excel spreadsheet. I don't like batch files automatically deleting stuff!

dir /b /a-d *.* > filelist.txt

However, I think what you're looking for is the FOR command. Type HELP FOR at the CMD prompt for a full description. It's very powerful and quite complicated, but I'm sure you can figure it out. Post here when / if you do because I might come back and look in more detail later on.

You can do it with a .vbs script too, which might actually be easier.

Good luck!

_________________
jonbwfc's law: "In any forum thread someone will, no matter what the subject, mention Firefly."

When you're feeling too silly for x404, youRwired.net


Wed May 06, 2009 1:33 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
OK, I've been exploring the for statement in the windows command line.

I now know how to find all the .r files in the current directory and all sub directories.

BUT!

I have a variable %X which will be something like... C:\somedirectory\subfolder\file.r

I now want to do an if exists statement with %X but using "file.*" instead of "file.r" at the end of the file path if that makes sense.

Hmm... any ideas?

Thanks

P.S. I've got an idea, back in a sec.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Fri May 08, 2009 9:20 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Done :D

This is the line that does it...

Code:
for /r E:\ofoggi\gui %%X in (*.r) do if not exist %%~dX%%~pX%%~nX.p if not exist %%~dX%%~pX%%~nX.w echo %%X >> C:\listoffiles.txt


The syntax is as follows...

Code:
for (list of items) in (set) do (command)


i.e. mine does this

for E:\ofoggi\gui and all it's subfolders find all files ending in .r do search for same file but ending in .p if it isn't there search for the same file ending in .w if that isn't there then write the file name to a txt file.

The hardest bit to find was the variable editing stuff.

The file path is %%X

%%~dX will return the drive letter i.e. "E:"
%%~pX will return the path i.e. "\ofoggi\gui\subfolder\"
%%~nX will return the filename without the extension i.e. "somefile"

(All this came from HERE)

I may change the echo to just del the files instead.

If you are doing this from a command line (not a batch file) then only use single %s. doubles %s are used for batch files.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Fri May 08, 2009 10:42 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:58 pm
Posts: 8767
Location: behind the sofa
Reply with quote
Well done. Knew you'd get it to work once you knew where to start ;)

And thanks for posting back. That's pretty clever stuff :D

_________________
jonbwfc's law: "In any forum thread someone will, no matter what the subject, mention Firefly."

When you're feeling too silly for x404, youRwired.net


Sat May 09, 2009 1:16 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
I've edited the code a bit to make it a bit better :D

Code:
@echo off

:start
CLS
if exist "C:\List of .r files.txt" del "C:\List of .r files.txt"

:input
set INPUT=
set LOOP=1
set /P INPUT=Enter the name of the user folder you wish to search or enter "all" to search all user folders %=%
if "%INPUT%"=="" goto input
if "%INPUT%"=="all" goto all
set FILEPATH=E:\%INPUT%
goto search

:all
if %LOOP%==1 set FILEPATH=E:\ofoggi
if %LOOP%==2 set FILEPATH=E:\cricha
if %LOOP%==3 set FILEPATH=E:\jstott
if %LOOP%==4 set FILEPATH=E:\tbasti
if %LOOP%==5 set FILEPATH=E:\awatso
if %LOOP%==6 set FILEPATH=E:\majohn
if %LOOP%==7 set FILEPATH=E:\mawils
if %LOOP%==8 goto results

:search
echo Searching gui and hire folders in %FILEPATH% for .r files with no source files...

for /r %FILEPATH%\gui %%X in (*.r) do if not exist %%~dX%%~pX%%~nX.p if not exist %%~dX%%~pX%%~nX.w echo %%X >> "C:\List of .r files.txt"

for /r %FILEPATH%\hire %%X in (*.r) do if not exist %%~dX%%~pX%%~nX.p if not exist %%~dX%%~pX%%~nX.w echo %%X >> "C:\List of .r files.txt"

set /a LOOP=%LOOP%+1
if "%INPUT%"=="all" goto all

:results
if exist "C:\List of .r files.txt" goto :found else goto :notfound

:notfound
echo No files found.
pause
goto restart

:found
echo Files found, press enter to open list of files.
pause
"C:\List of .r files.txt"
goto restart

:restart
set RESTART=
set /P RESTART=Do you wish to search again? Y/N %=%
if "%RESTART%"=="Y" goto start
if "%RESTART%"=="y" goto start


It would be better if I knew how to ask a simple yes no question without having to input to a variable though.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed May 13, 2009 1:16 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
Made another change to streamline it a bit and I think I'm going to leave it alone now :D

Code:
@echo off

:start
CLS
if exist "C:\List of .r files.bat" del "C:\List of .r files.bat"

:input
set INPUT=
set LOOP=1
set /P INPUT=Enter the name of the user folder you wish to search or enter "all" to search all user folders %=%
if "%INPUT%"=="" goto input
if "%INPUT%"=="all" goto all
set FILEPATH=E:\%INPUT%
goto search

:all
if %LOOP%==1 set FILEPATH=E:\ofoggi
if %LOOP%==2 set FILEPATH=E:\cricha
if %LOOP%==3 set FILEPATH=E:\jstott
if %LOOP%==4 set FILEPATH=E:\tbasti
if %LOOP%==5 set FILEPATH=E:\awatso
if %LOOP%==6 set FILEPATH=E:\majohn
if %LOOP%==7 set FILEPATH=E:\mawils
if %LOOP%==8 goto results

:search
echo Searching gui and hire folders in %FILEPATH% for .r files with no source files...

for /r %FILEPATH%\gui %%X in (*.r) do if not exist %%~dX%%~pX%%~nX.p if not exist %%~dX%%~pX%%~nX.w echo del /p %%X >> "C:\List of .r files.bat"

for /r %FILEPATH%\hire %%X in (*.r) do if not exist %%~dX%%~pX%%~nX.p if not exist %%~dX%%~pX%%~nX.w echo del /p %%X >> "C:\List of .r files.bat"

set /a LOOP=%LOOP%+1
if "%INPUT%"=="all" goto all

:results
if exist "C:\List of .r files.bat" goto :found else goto :notfound

:notfound
echo No files found.
pause
goto restart

:found
echo Files found...
call "C:\List of .r files.bat"
goto restart

:restart
set RESTART=
set /P RESTART=Do you wish to search again? Y/N %=%
if "%RESTART%"=="Y" goto start
if "%RESTART%"=="y" goto start


It now creates a batch file with a load of "del /p filename" lines in it. del /p will prompt before deleting.

It then goes off and calls that file so that once it finishes it then carries on in this file. If you just run the file then it closes this file and runs that in it's place i.e. you don't get the restart prompt.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue May 19, 2009 3:17 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.