Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
ni.va
- Posts: 6
- Joined: 09 Mar 2011 05:56
#1
Post
by ni.va » 09 Mar 2011 06:11
Hi,
I have around 2000 pdf files that each must go to a different folder. I made a list of them containing old file names, & new file path & names (like : --\--.pdf) the path is in 3 levels. can I move & rename files using this list? I don't have any scriping knowledge
All of my files are under 4 sub directories under directory named "Auditing Reports"
example of my List.txt:
Code: Select all
OldName|NewPath
001S.pdf|D:\Auditing Reports\A\B\S-88.pdf
002S.pdf|D:\Auditing Reports\C\D\S-87.pdf
I created the list using excel so there is no relation between old & new file names.
Thanks In Advance
Edit: Answer provided by !k:pattern of list.txt:
Code: Select all
file name|source directory\|destination directory
Code in batch file:
Code: Select all
for /f "tokens=1,2,3 delims=|" %%f in (List.txt) do (
if "%%~xh"=="" (md "%%h" 2>nul) else md "%%~dph" 2>nul
move "%%g%%f" "%%h"
)
Notes:
- if you dont have source directory:
pattern of list.txt
Code in batch file:
Code: Select all
for /f "tokens=1,2 delims=|" %%f in (List.txt) do (
if "%%~xg"=="" (md "%%h" 2>nul) else md "%%~dpg" 2>nul
move "%%f" "%%g"
)
OR you can simply put two quotes ("") replacing "source directory\" above
- destination directory can be a path with new file name
Last edited by
ni.va on 12 Mar 2011 08:39, edited 3 times in total.
-
!k
- Expert
- Posts: 378
- Joined: 17 Oct 2009 08:30
- Location: Russia
#2
Post
by !k » 09 Mar 2011 09:07
Code: Select all
for /f "tokens=1,2 delims=|" %%f in (List.txt) do (
md "%%~dpg" 2>nul
move "%%f" "%%g"
)
Last edited by
!k on 12 Mar 2011 05:52, edited 1 time in total.
-
ni.va
- Posts: 6
- Joined: 09 Mar 2011 05:56
#3
Post
by ni.va » 12 Mar 2011 04:50
Thanks SO MUCH!!! It worked. but did not create the destination folders.
I had to create the folders manually...
-
ni.va
- Posts: 6
- Joined: 09 Mar 2011 05:56
#4
Post
by ni.va » 12 Mar 2011 05:53
My Problem has been solved.
for those who have lists like:
Code: Select all
file name|source directory|destination directory
can use this cammand. it will create destination folder & I added the second line to move the files.
Code: Select all
for /f "tokens=1,2,3 delims=|" %%f in (List.txt) do xcopy "%%g\%%f" "%%h" /-y
for /f "tokens=1,2,3 delims=|" %%f in (List.txt) do move "%%g\%%f" "%%h"
-
!k
- Expert
- Posts: 378
- Joined: 17 Oct 2009 08:30
- Location: Russia
#5
Post
by !k » 12 Mar 2011 05:57
ni.va wrote:create the destination folders
Added
-
!k
- Expert
- Posts: 378
- Joined: 17 Oct 2009 08:30
- Location: Russia
#6
Post
by !k » 12 Mar 2011 06:06
ni.va wrote:xcopy
move
It's superfluous.
Use
Code: Select all
for /f "tokens=1,2,3 delims=|" %%f in (List.txt) do (
md "%%h" 2>nul
move "%%g\%%f" "%%h"
)
or
Code: Select all
for /f "tokens=1,2,3 delims=|" %%f in (List.txt) do (
echo D|(xcopy "%%g\%%f" "%%h" &&del /q "%%g\%%f")
)
-
ni.va
- Posts: 6
- Joined: 09 Mar 2011 05:56
#7
Post
by ni.va » 12 Mar 2011 06:38
Thanks,
but %%h may be file path with extension,
So both codes will result ...\1.txt\1.txt.
-
!k
- Expert
- Posts: 378
- Joined: 17 Oct 2009 08:30
- Location: Russia
#8
Post
by !k » 12 Mar 2011 07:22
Code: Select all
for /f "tokens=1,2,3 delims=|" %%f in (List.txt) do (
if "%%~xh"=="" (md "%%h" 2>nul) else md "%%~dph" 2>nul
move "%%g\%%f" "%%h"
)
-
ni.va
- Posts: 6
- Joined: 09 Mar 2011 05:56
#9
Post
by ni.va » 12 Mar 2011 07:28
Thanks, that's working correctly.
Updated first post, Correct me if I'm wrong...
-
alperefe
- Posts: 9
- Joined: 20 Jun 2012 21:22
#10
Post
by alperefe » 20 Jun 2012 21:27
I am trying to use this code in a batch. but it gives "echo was unexpected at this time" kinda error. I ve tried ENABLEDELAYEDEXPANSION stuff but it didnt make a difference. can you post complete batch file please?
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#11
Post
by foxidrive » 20 Jun 2012 21:53
Shows us a few lines of your list, and describe which segment is which.
-
Ed Dyreen
- Expert
- Posts: 1569
- Joined: 16 May 2011 08:21
- Location: Flanders(Belgium)
-
Contact:
#13
Post
by Ed Dyreen » 28 Jun 2012 02:24
alperefe wrote:I am trying to use this code in a batch. but it gives "echo was unexpected at this time" kinda error. I ve tried ENABLEDELAYEDEXPANSION stuff but it didnt make a difference. can you post complete batch file please?
That's a simple syntax error, replace @echo off with @echo on, place a pause at the end of your batch, move it up until the error disappears, disassemble the function, simplify it and post it so we can help u.
btw: to debug a batch create another batch and call it like this, that will prevent the window from closing before
you have the chance to read it...Code: Select all
@start "cmd" "%comspec%" /k "%~dp0myBatch.CMD"
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#14
Post
by foxidrive » 28 Jun 2012 02:50
Or show us a few lines of your list so we can create something that will work, ya dodo!
-
alperefe
- Posts: 9
- Joined: 20 Jun 2012 21:22
#15
Post
by alperefe » 28 Jun 2012 03:51
actually I am testing the batch as it is in the first post, so my txt setup is the exact same. folders are same etc. I removed the enabledexpansions stuff and now it shows me the code but doesnt do anything.