BAT command to manipulate string

User avatar
Jezza
5StarLounger
Posts: 847
Joined: 24 Jan 2010, 06:35
Location: A Magic Forest in Deepest, Darkest, Kent

BAT command to manipulate string

Post by Jezza »

I have a batch file that reads from a text file containing file locations

FOR /F "tokens=1" %%i in (PCN.txt) DO XCOPY %%i C:\Users\jezza\Desktop\DataMove\out

PCN.txt will hold file locations thus (79,000 files in various folder locations):

X:\DES\DES-TSL\Attachments\20050526\00547428.tif
X:\DES\DES-TSL\Attachments\20050525\00547434.tif
X:\DES\DES-TSL\Attachments\20050524\00547469.tif
X:\DES\DES-TSL\Attachments\20050524\00547479.tif
X:\DES\DES-TSL\Attachments\20050524\00547486.tif

I would like to move these files a folder down C:\Users\jezza\Desktop\DataMove\out\backup but creating/keeping the folder structure within out on my C: drive

My head is in a spin as I cannot seem to manipulate %%i, can anyone assist?
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it

User avatar
Rudi
gamma jay
Posts: 25455
Joined: 17 Mar 2010, 17:33
Location: Cape Town

Re: BAT command to manipulate string

Post by Rudi »

I'll give the batch file editing a wide berth and use a UI app to do the work.
If you (or anyone else is interested), have a look at the app called: File Bucket, homepage and here for description
Regards,
Rudi

If your absence does not affect them, your presence didn't matter.

User avatar
John Gray
PlatinumLounger
Posts: 5411
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: BAT command to manipulate string

Post by John Gray »

As always, the difficulty is with the specification of what you want to do.
From what I read, surely it is just as simple as running:
FOR /F "tokens=1" %%i in (PCN.txt) DO XCOPY %%i C:\Users\jezza\Desktop\DataMove\out\backup
John Gray

"(or one of the team)" - how your hospital appointment letter indicates that you won't be seeing the Consultant...

User avatar
Jezza
5StarLounger
Posts: 847
Joined: 24 Jan 2010, 06:35
Location: A Magic Forest in Deepest, Darkest, Kent

Re: BAT command to manipulate string

Post by Jezza »

Hi John

Yes, that would copy all the files just into the backup folder but these files will be coming from numerous subfolders, I wanted to preserve that folder structure as so it would look something like:

C:\Users\jezza\Desktop\DataMove\out\backup\20050526\00547428.tif
C:\Users\jezza\Desktop\DataMove\out\backup\20050525\00547434.tif

so for an example take the string value below
X:\DES\DES-TSL\Attachments\20050526\00547428.tif

set a variable to get:
X:\DES\DES-TSL\Attachments\20050526

convert it to:
C:\Users\jezza\Desktop\DataMove\out\backup\20050526

Make a directory at the new location

Copy the file in X:\DES\DES-TSL\Attachments\20050526\00547428.tif to C:\Users\jezza\Desktop\DataMove\out\backup\20050526

Repeat with next %%i

I hope that is clear
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it

User avatar
John Gray
PlatinumLounger
Posts: 5411
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: BAT command to manipulate string

Post by John Gray »

Ah, I see.

Will there ALWAYS be the same number of levels in the source path and the target path?

In other words, are the paths always
X:\DES\DES-TSL\Attachments\<somedir>\<somefile.tif> ?
C:\Users\jezza\Desktop\DataMove\out\backup\<somedir>\<somefile.tif> ?

And will all paths always be complete free from blanks?
John Gray

"(or one of the team)" - how your hospital appointment letter indicates that you won't be seeing the Consultant...

User avatar
John Gray
PlatinumLounger
Posts: 5411
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: BAT command to manipulate string

Post by John Gray »

Based on my assumptions above, I have tested it as far as I can with:

PCN.txt

Code: Select all

X:\DES\DES-TSL\Attachments\direct01\fileaaaa.tif
X:\DES\DES-TSL\Attachments\direct02\filebbbb.tif
X:\DES\DES-TSL\Attachments\direct03\filecccc.tif
X:\DES\DES-TSL\Attachments\direct04\filedddd.tif
X:\DES\DES-TSL\Attachments\direct05\fileeeee.tif
JezzaCop.bat

Code: Select all

@echo off

:: from    X:\DES\DES-TSL\Attachments\<somedir>\<somefile.tif> 
:: tokens  1  2   3       4           5         6

:: to   C:\Users\jezza\Desktop\DataMove\out\backup\<somedir>\<somefile.tif>

setlocal

for /f "tokens=1-6 delims=\ " %%a in (PCN.txt) do call :process %%a %%b %%c %%d %%e %%f

endlocal
goto :eof
:--------------------------------------------------------------------------------------

:process

set srcpath=%1\%2\%3\%4
set srcdir=%5
set srcfile=%6

set tgtpath=C:\Users\jezza\Desktop\DataMove\out\backup
set tgtdir=%srcdir%

echo copying %srcpath%\%srcdir%\%srcfile% to %tgtpath%\%tgtdir% 

:: remove the REM from the next line once you're happy with what's happening!
REM xcopy %srcpath%\%srcdir%\%srcfile% %tgtpath%\%tgtdir%

goto :eof
Console log:

Code: Select all

copying X:\DES\DES-TSL\Attachments\direct01\fileaaaa.tif to C:\Users\jezza\Desktop\DataMove\out\backup\direct01 
copying X:\DES\DES-TSL\Attachments\direct02\filebbbb.tif to C:\Users\jezza\Desktop\DataMove\out\backup\direct02 
copying X:\DES\DES-TSL\Attachments\direct03\filecccc.tif to C:\Users\jezza\Desktop\DataMove\out\backup\direct03 
copying X:\DES\DES-TSL\Attachments\direct04\filedddd.tif to C:\Users\jezza\Desktop\DataMove\out\backup\direct04 
copying X:\DES\DES-TSL\Attachments\direct05\fileeeee.tif to C:\Users\jezza\Desktop\DataMove\out\backup\direct05 
Try it on your PCN.txt and see what you get. When happy, remove the REM in the third-last line of JezzaCop.bat.
John Gray

"(or one of the team)" - how your hospital appointment letter indicates that you won't be seeing the Consultant...

User avatar
Jezza
5StarLounger
Posts: 847
Joined: 24 Jan 2010, 06:35
Location: A Magic Forest in Deepest, Darkest, Kent

Re: BAT command to manipulate string

Post by Jezza »

Good Lord, I was going down the completely wrong track! I will have go with it later on but this looks like the baby, thank very much :thankyou:
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it

User avatar
John Gray
PlatinumLounger
Posts: 5411
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: BAT command to manipulate string

Post by John Gray »

If you get a silly message coming up, asking whether you're copying to a file or to a directory, add a \ to the end of the two lines near the bottom, after %tgtdir%
John Gray

"(or one of the team)" - how your hospital appointment letter indicates that you won't be seeing the Consultant...

User avatar
Jezza
5StarLounger
Posts: 847
Joined: 24 Jan 2010, 06:35
Location: A Magic Forest in Deepest, Darkest, Kent

Re: BAT command to manipulate string

Post by Jezza »

John, I just wanted to add my thanks for the code you have written, it works a dream and I am currently copying 73925 files of all shapes and sizes
Jerry
I’ll be more enthusiastic about encouraging thinking outside the box when there’s evidence of any thinking going on inside it

User avatar
John Gray
PlatinumLounger
Posts: 5411
Joined: 24 Jan 2010, 08:33
Location: A cathedral city in England

Re: BAT command to manipulate string

Post by John Gray »

Excellent! As I said, it is often just a matter of understanding the requirements successfully
John Gray

"(or one of the team)" - how your hospital appointment letter indicates that you won't be seeing the Consultant...