eyeball Posted January 10, 2006 Share Posted January 10, 2006 Hi All,i need an example if someone could give me one, of how to check if a file exists, and if it does do nothing however if it doesnt exist i need to then perform an action (copy a file from one drive to another for example)i hope this is making sense, like i say i just need a push in the right direction and i can do the restthanks guys btw i dont mind if its command line or vb script based, which ever is easiest Link to comment Share on other sites More sharing options...
SilverBulletUK Posted January 10, 2006 Share Posted January 10, 2006 (edited) Hello!You could try something like this... Simple, but easily changed.@Echo OffSet File=C:\Testfile.docClsEcho.Echo Testing if file exists...Echo.If exist %file% goto :YESFILEEcho Sorry, the file "%file%", does not exist...Echo.PauseExit:YESFILEEcho Success, file "%file%" exists!Echo.PauseExitAny use mateee? Save as filetest.BAT Edited January 10, 2006 by SilverBulletUK Link to comment Share on other sites More sharing options...
No6 Posted January 10, 2006 Share Posted January 10, 2006 Hi All,i need an example if someone could give me one, of how to check if a file exists, and if it does do nothing however if it doesnt exist i need to then perform an action (copy a file from one drive to another for example)i hope this is making sense, like i say i just need a push in the right direction and i can do the restthanks guys btw i dont mind if its command line or vb script based, which ever is easiest Very short one-liner:@if not exist c:\folder1\filename.ext copy c:\folder2\filename.ext c:\folder1Substitute the obvious. Link to comment Share on other sites More sharing options...
eyeball Posted January 11, 2006 Author Share Posted January 11, 2006 thank you guys thats exactly what i needed cheers! Link to comment Share on other sites More sharing options...
Mr Snrub Posted January 11, 2006 Share Posted January 11, 2006 Don't forget to use quotes to make sure paths or filenames with spaces are handled okay, e.g.:@if exist "C:\Documents and Settings\MyUser\Desktop\blah.lnk" copy "C:\Some Folder\A file.exe" "C:\Some other folder\"or with the other example:...if exist "%file%" goto YESFILE... Link to comment Share on other sites More sharing options...
TheFlash428 Posted January 11, 2006 Share Posted January 11, 2006 (edited) vb script:Set objFSO = CreateObject("Scripting.FileSystemObject")strFile = "[path to file]"If objFSO.FileExists(strFile) ThenWScript.QuitElse[action to perform]End IfWscript.QuitThis will accomplish what you are looking for--I could help with the rest if you let me know what action you wish to perform. Edited January 11, 2006 by TheFlash428 Link to comment Share on other sites More sharing options...
eyeball Posted January 11, 2006 Author Share Posted January 11, 2006 (edited) iv just tried this and realised iv explained my problem wrongly to you all. sorry ill start againlets say i have 2 foldersfolder 1 contains:1.jpg2.jpg3.jpgfolder 2 contains:1.jpg2.jpg(obviously my files arent 1,2,3 etc) but i want the script to compare the contents of the 2 folders and if they are different copy the missing filesin this example it would be 3.jpgis this possible please?thank you Edited January 11, 2006 by eyeball Link to comment Share on other sites More sharing options...
Mr Snrub Posted January 11, 2006 Share Posted January 11, 2006 (edited) @echo offset SRCDIR=C:\Example Source Folderset DSTDIR=E:\Folder Where I Want To Copy Stuff Tofor /f %%a in ('dir /b "%SRCDIR%"') do ( if not exist "%DSTDIR%\%%a" echo Copying %%a if not exist "%DSTDIR%\%%a" copy "%SRCDIR%\%%a" "%DSTDIR%" > nul)That should do you, and report each file it copies from SRCDIR to DSTDIR, you just need to set the variables accordingly.If you want to get really advanced with error checking you could ensure that SRCDIR and DSTDIR actually exist before starting the FOR loop. Edited January 11, 2006 by Mr Snrub Link to comment Share on other sites More sharing options...
eyeball Posted January 11, 2006 Author Share Posted January 11, 2006 (edited) thanks mr snub thats exactly what i want but it gives me an error and im not that advanced at vbs scripting, i can understand why it does what it does but i couldnt debug it lol. please help i have the following..@echo offset SRCDIR = "f:\test1"set DSTDIR = "f:\test2"for /f %%a in ('dir /b "%SRCDIR%"') do ( if not exist "%DSTDIR%\%%a" echo Copying %%a if not exist "%DSTDIR%\%%a" copy "%SRCDIR%\%%a" "%DSTDIR%" > nul) Edited January 11, 2006 by eyeball Link to comment Share on other sites More sharing options...
Mr Snrub Posted January 11, 2006 Share Posted January 11, 2006 set SRCDIR = "f:\test1"set DSTDIR = "f:\test2"No quotes in the SET statements, and no spaces either side of the equals sign. Link to comment Share on other sites More sharing options...
eyeball Posted January 11, 2006 Author Share Posted January 11, 2006 thanks again, but there is still an error god i feel like a dumbass thanks for the help i have..set SRCDIR=f:\test1set DSTDIR=f:\test2for /f %%a in ('dir /b "%SRCDIR%"') do ( if not exist "%DSTDIR%\%%a" echo Copying %%a if not exist "%DSTDIR%\%%a" copy "%SRCDIR%\%%a" "%DSTDIR%" > nul)any ideas please?thanks Link to comment Share on other sites More sharing options...
Mr Snrub Posted January 11, 2006 Share Posted January 11, 2006 (edited) set SRCDIR=f:\test1set DSTDIR=f:\test2for /f %%a in ('dir /b "%SRCDIR%"') do ( if not exist "%DSTDIR%\%%a" echo Copying %%a if not exist "%DSTDIR%\%%a" copy "%SRCDIR%\%%a" WHAT HAPPENED HERE?"%DSTDIR%" > nul)Looks like you hit ENTER a couple of times in the middle of a line.Check back at my example, there should just be 2 lines between the brackets, both starting with "if".@echo offset SRCDIR=f:\test1set DSTDIR=f:\test2for /f %%a in ('dir /b "%SRCDIR%"') do ( if not exist "%DSTDIR%\%%a" echo Copying %%a if not exist "%DSTDIR%\%%a" copy "%SRCDIR%\%%a" "%DSTDIR%" > nul)That should be what you have in a batch file. Edited January 11, 2006 by Mr Snrub Link to comment Share on other sites More sharing options...
eyeball Posted January 11, 2006 Author Share Posted January 11, 2006 thanks again and sorry about that! lol just tried that and it doesnt work :si dragged it into a cmd window and it looks like its copying but when i check the dstdir there is nothing in there Link to comment Share on other sites More sharing options...
Mr Snrub Posted January 11, 2006 Share Posted January 11, 2006 Copy/paste the contents of the last code window into Notepad, then save as "test.bat" (make sure you put quotes around the filename when saving, or change the filetype to "All Files", otherwise ".txt" gets appended to the end).Double-click on the test.bat icon and it should do its job.If you need to edit the batch file, right-click on it and click Edit to open it in Notepad again. Link to comment Share on other sites More sharing options...
eyeball Posted January 11, 2006 Author Share Posted January 11, 2006 iv done that and still no luck i put a .txt file in f:\test1 and nothing in f:\test2 and ran the batch file but nothing was copied over.if i put the batch in f:\test1 and run it only the .bat file is copied to f:\test2 not the .txt file! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now