Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Batch File: FOR /R Copy file with exclamation point in the name

Posted on 2010-08-30
13
Medium Priority
?
1,334 Views
Last Modified: 2012-05-10
Hi there,

By using a FOR /R command as my provided script, I need to copy files that some times, will have an exclamation mark in the name.

I tried to add an escape code to it, [SET FileName=!FileName:^!=^!!] but I guess I was too ambitious here.

Thanks for your help,
Rene






@echo off
setlocal enabledelayedexpansion

SET SOURCE=C:\Batchfiles\TESTING\1
SET DESTIN=C:\Batchfiles\TESTING\2

for /R "%SOURCE%" %%A in (*.*) do (
	SET FileName=%%A
	ECHO COPYING !FileName!
	COPY /Y "!FileName!" "%DESTIN%"
	)
PAUSE
EXIT

Open in new window

0
Comment
Question by:ReneGe
  • 7
  • 6
13 Comments
 
LVL 59

Expert Comment

by:Bill Prew
ID: 33563413
Dealing with ! in strings like filenames in BAT files can be rather tricky.

In your original example it isn't possible, because the ! never actually gets into the FileName variable.  When the statement

SET FileName=%%A

executed, it replaces %%A with it's value, and then proceeds to parse the line.  It will see the ! point, and since you have enabled delayed expansion, it treats that as a special character and "eats it up",

For the example you provided, attached is a working solution.

~bp
@ECHO OFF
  
SET SOURCE=C:\Batchfiles\TESTING\1
SET DESTIN=C:\Batchfiles\TESTING\2
 
FOR /R "%SOURCE%" %%A IN (*.*) DO (
        ECHO COPYING %%A
        COPY /Y "%%A" "%DESTIN%"
        )
PAUSE
EXIT /B

Open in new window

0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 33563423
For what it's worth, using special characters in Windows file names other than a couple of safe ones like dash, underscore, space can often lead to undesirable consequences in different applications.  I typically try and avoid things like slashes, colons, exclamations, percents, etc.

~bp
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33563535
My challange here is that this script is part of a bigger script that delayed expansion must be enabled.

Let's say it must work with the following script.

Can then delayed expansion be turned off then on within the secong loop, or something?

Thanks,
Rene
@echo off
SETLOCAL enabledelayedexpansion

	SET SOURCE=C:\Batchfiles\TESTING\1
	SET DESTIN=C:\Batchfiles\TESTING\2

FOR /L %%A IN (1,1,2) DO (
	SET TIMES=%%A
	FOR /R "%SOURCE%" %%B IN (*.*) DO (
		SET FileName=%%B
		ECHO COPYING !TIMES! TIMES: %FileName%
		COPY /Y "%FileName%" "%DESTIN%"
	)
)

PAUSE
EXIT /B

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 59

Expert Comment

by:Bill Prew
ID: 33563550
Why not do it this way?

~bp
0
 
LVL 59

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 33563554
Why not do it this way?

~bp
@echo off

        SET SOURCE=C:\Batchfiles\TESTING\1
        SET DESTIN=C:\Batchfiles\TESTING\2

FOR /L %%A IN (1,1,2) DO (
        FOR /R "%SOURCE%" %%B IN (*.*) DO (
                ECHO COPYING %%A TIMES: %%B
                COPY /Y "%%~B" "%DESTIN%"
        )
)

PAUSE
EXIT /B

Open in new window

0
 
LVL 10

Author Comment

by:ReneGe
ID: 33563579
That should do it!

Thanks bp  :~)

Cheers,
Rene
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 33563585
Great, thanks.

~bp
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33568709
Well finally, I'm still challanged with it.

I dont see how I can use the values contained in the variables "SET BasePath_Dest=!BasePath_Dest%%A!" without using delayed expansion enabled.

Would you mind giving it a look and give me your feedback?

Thanks,
Rene
@echo off

SETLOCAL enabledelayedexpansion

REM SETTING STATIC VARIABLES (TheDate=!Year!-!Month!-!Day!) (TheTime=!Hour!.!Min!.!Sec!)
	CALL :GetDateTime "Month" "Day" "Year"
	SET TimeStart=%date%  %time%


ECHO ^>READING INI FILES
REM for /F "tokens=1,2 delims=^=" %%A in (Settings.ini) do SET Setting.ini_%%A=%%B
	SET CounterPath=0
	for /F "tokens=1-4 delims=^>" %%A in (Paths.ini) do (
		SET /a CounterPath=!CounterPath! + 1
		SET BasePathName=%%A
		SET BasePath_Source!CounterPath!=%%B
		SET BasePath_Dest!CounterPath!=%%C
		SET BasePath_Archives!CounterPath!=%%D
	)



etc...


	
	FOR /L %%A in (1,1,%CounterPath%) do (
		ECHO  ------------------------------------------------------------
		REM SETUP BASE FOLDERS
			SET BasePath_Source=!BasePath_Source%%A!
			SET BasePath_Dest=!BasePath_Dest%%A!
			SET BasePath_Archives=!BasePath_Archives%%A!\%Year%\%Month%\%Day%-%Hour%h
			IF NOT EXIST "!BasePath_Archives!" MD "!BasePath_Archives!"
			IF NOT EXIST "!BasePath_Dest!" MD "!BasePath_Dest!"
			ECHO COUNTER=%%A  BasePath_Source=!BasePath_Source!  BasePath_Dest=!BasePath_Dest!  BasePath_Archives=!BasePath_Archives!
			FOR /F "TOKENS= 2 DELIMS=	" %%A IN ('Robocopy "!BasePath_Dest!" "!BasePath_Source!" * /E /MIR /L /NJH /NJS /NS /NDL /XF *.mp3 *.wav *.avi *.wma *.iso /XD ISO "Virtual Machines *" Musique test ^| Findstr -V ^*') DO (
				SET PathSource=%%~pA
				SET SourceSize=%%~zA
				SET FileFull=%%A
				SET ZipFile=!BasePath_Archives!%%~spnA.zip
				Echo %TheDate%^>%TheTime%^>!SourceSize!^>!FileFull!^>!ZipFile!>>"%File_Archives_Index%"
				ECHO.
				ECHO ARCHIVING: !FileFull!
				ECHO        TO: !ZipFile!
				"C:\Program Files\7-Zip\7z.exe" a -ppatoF645 -mx=9 "!ZipFile!" "!FileFull!">>"%File_LOG_Archives%_temp.TXT"
				SET Result=!errorlevel!
				IF !Result! NEQ 0 (
					ECHO !Result! ---^> "!FileFull!" >>%File_LOG_ArchivesError%
					ECHO ARCHIVE ERROR
				) ELSE (
					ECHO ARCHIVE COMPLETED
				)
			)
		)
ECHO ^>CLEANING %File_LOG_Archives%
	IF EXIST "%File_LOG_Archives%_temp.TXT" (
		FOR /F "DELIMS=" %%A IN ('TYPE "%File_LOG_Archives%_temp.TXT" ^| FindStr -i -v "7-Zip 4.65  Copyright" ^| Findstr -i -l -x -v "Scanning"') do echo %%A>>"%File_LOG_Archives%"
	)


etc...

Open in new window

0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 33568844
In your example above, which file or pathnames could contain exclamation points?

~bp
0
 
LVL 10

Author Comment

by:ReneGe
ID: 33569389
This portion of the script will archive (zip) files to be later on deleted or updated from the source on a further Robocopy mirroring.

Robocopy will output the file list to -->%%A  then  --> !FileFull!

Therefore, the files containing the ! are put by users anytime/anywhere in the !BasePath_Source!\*.* /S

Thanks,
Rene
FOR /F "TOKENS= 2 DELIMS=	" %%A IN ('Robocopy "!BasePath_Dest!" "!BasePath_Source!" * /E /MIR /L /NJH /NJS /NS /NDL /XF *.mp3 *.wav *.avi *.wma *.iso /XD ISO "Virtual Machines *" Musique test ^| Findstr -V ^*') DO (
				SET PathSource=%%~pA
				SET SourceSize=%%~zA
				SET FileFull=%%A

Open in new window

0
 
LVL 10

Author Comment

by:ReneGe
ID: 33570719
I came up with the following sample working script.

Do you think this should be by best approach?

Thanks & cheers,
Rene

@echo off
SETLOCAL enabledelayedexpansion

SET SOURCE=C:\Batchfiles\TESTING\1
SET DESTIN1=C:\Batchfiles\TESTING\2a
SET DESTIN2=C:\Batchfiles\TESTING\2b

FOR /L %%A IN (1,1,2) DO CALL :CopyFiles "!DESTIN%%A!"

PAUSE
EXIT /B


:CopyFiles
SETLOCAL disabledelayedexpansion

SET DESTIN=%~1

IF NOT EXIST "%DESTIN%" MD "%DESTIN%"
FOR /R "%SOURCE%" %%A IN (*.*) DO (
	ECHO COPYING "%%A"  TO  "%DESTIN%"
	COPY /Y "%%A" "%DESTIN%" 
)

SETLOCAL enabledelayedexpansion
EXIT /b

Open in new window

0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 33605172
How about this approach to keep it even simpler?

~bp
@ECHO OFF
SET SOURCE=C:\Batchfiles\TESTING\1
SET DESTIN="C:\Batchfiles\TESTING\2a","C:\Batchfiles\TESTING\2b"
FOR %%D IN (%DESTIN%) DO IF NOT EXIST "%%~D" MD "%%~D"
FOR /R "%SOURCE%" %%S IN (*.*) DO (
  FOR %%D IN (%DESTIN%) DO (
    ECHO COPYING "%%S"  TO  "%%~D"
    COPY /Y "%%S" "%%~D" 
  )
)
PAUSE
EXIT /B

Open in new window

0
 
LVL 10

Author Comment

by:ReneGe
ID: 33605252
Nice...

Thanks bp !

Cheers,
Rene
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question