Jump to content

If Statements in a script


Recommended Posts

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 rest

thanks guys :)

btw i dont mind if its command line or vb script based, which ever is easiest :thumbup

Link to comment
Share on other sites


Hello!

You could try something like this... Simple, but easily changed.

@Echo Off

Set File=C:\Testfile.doc

Cls
Echo.
Echo Testing if file exists...
Echo.
If exist %file% goto :YESFILE
Echo Sorry, the file "%file%", does not exist...
Echo.
Pause
Exit

:YESFILE
Echo Success, file "%file%" exists!
Echo.
Pause
Exit

Any use mateee? :blushing:

Save as filetest.BAT ;)

Edited by SilverBulletUK
Link to comment
Share on other sites

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 rest

thanks guys :)

btw i dont mind if its command line or vb script based, which ever is easiest :thumbup

Very short one-liner:

@if not exist c:\folder1\filename.ext copy c:\folder2\filename.ext c:\folder1

Substitute the obvious.

Link to comment
Share on other sites

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

vb script:

Set objFSO = CreateObject("Scripting.FileSystemObject")

strFile = "[path to file]"

If objFSO.FileExists(strFile) Then
WScript.Quit
Else
[action to perform]
End If

Wscript.Quit

This 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 by TheFlash428
Link to comment
Share on other sites

iv just tried this and realised iv explained my problem wrongly to you all. sorry ill start again

lets say i have 2 folders

folder 1 contains:

1.jpg

2.jpg

3.jpg

folder 2 contains:

1.jpg

2.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 files

in this example it would be 3.jpg

is this possible please?

thank you

Edited by eyeball
Link to comment
Share on other sites

@echo off
set SRCDIR=C:\Example Source Folder
set DSTDIR=E:\Folder Where I Want To Copy Stuff To

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
)

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 by Mr Snrub
Link to comment
Share on other sites

thanks mr snub thats exactly what i want :)

but it gives me an error :blink: 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 off

set 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 by eyeball
Link to comment
Share on other sites

thanks again, but there is still an error :blink: god i feel like a dumbass thanks for the help :)

i have..

set 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

)

any ideas please?

thanks

Link to comment
Share on other sites

set 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"

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 off
set 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
)

That should be what you have in a batch file.

Edited by Mr Snrub
Link to comment
Share on other sites

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...