Link to home
Start Free TrialLog in
Avatar of E=mc2
E=mc2Flag for Canada

asked on

.BAT script to to move any files if files exist in path

I am having trouble with the following script:


pushd "C:\Users\Public\Documents\Emergency-Doc\Doc-Main\ReceivedRD\"
if exist *.* move *.* "C:\Users\Public\Documents\Emergency-Doc\Doc-Main\NewDoc\Inbound"
popd

Open in new window


I get an error message which says:

The filename, directory name, or volume label syntax is incorrect.


Both paths are accurate and exist. 


Any assisance would be very much appreciated. 

ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Maybe you need to put the entire command argument of the if statement in parenthesis

if exist *.* (move *.* "C:\Users\Public\Documents\Emergency-Doc\Doc-Main\NewDoc\Inbound")

Open in new window

Avatar of oBdA
oBdA

And since you have PowerShell in the Topics, here's the PS version.
In test mode as well, remove the -WhatIf at the end to run it for real:
Get-ChildItem -Path "C:\Users\Public\Documents\Emergency-Doc\Doc-Main\ReceivedRD" -File |
	Move-Item -Destination "C:\Users\Public\Documents\Emergency-Doc\Doc-Main\NewDoc\Inbound" -Verbose -WhatIf

Open in new window

Way too much overthinking


pushd "C:\Users\Public\Documents\Emergency-Doc\Doc-Main\ReceivedRD\" i
move * "C:\Users\Public\Documents\Emergency-Doc\Doc-Main\NewDoc\Inbound"
popd 

Open in new window

is all that is required. note the use of * vice *.* as well
Which will, if the folder is empty, produce the exact same error message that the poster wants to get rid of:
C:\Temp\Foo>dir /b

C:\Temp\Foo>move * D:\temp
The filename, directory name, or volume label syntax is incorrect.
C:\Temp\Foo>

Open in new window

No to mention that, like the original, it will wreak havoc with whatever the current directory is if the pushd fails for whatever reason. Making sure that a script doesn't have possibly really bad consequences because a pushd/cd failed is not what I would call "overthinking".
Avatar of E=mc2

ASKER

@oBdA,

When I execute the batch script what happens is that if there are no files to be copied it just exits the script and does not continue with the rest of the script..
Is this what this does?
Yes. To continue, use if/else:
pushd "C:\Users\Public\Documents\Emergency-Doc\Doc-Main\ReceivedRD"
dir /a:-d /b 2>NUL | find.exe /v "" >NUL
if errorlevel 1 (
	echo Found no files to move.
) else (
	ECHO move *.* "C:\Users\Public\Documents\Emergency-Doc\Doc-Main\NewDoc\Inbound"
)
popd

Open in new window