Page 1 of 2

Batch moving using text file

Posted: 09 Mar 2011 06:11
by ni.va
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: Select all

    file name|destination directory


    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

Re: Batch moving using text file

Posted: 09 Mar 2011 09:07
by !k

Code: Select all

for /f "tokens=1,2 delims=|" %%f in (List.txt) do (
  md "%%~dpg" 2>nul
  move "%%f" "%%g"
)

Re: Batch moving using text file

Posted: 12 Mar 2011 04:50
by ni.va
Thanks SO MUCH!!! It worked. but did not create the destination folders.
I had to create the folders manually...

Re: Batch moving using text file

Posted: 12 Mar 2011 05:53
by ni.va
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"

Re: Batch moving using text file

Posted: 12 Mar 2011 05:57
by !k
ni.va wrote:create the destination folders

Added

Re: Batch moving using text file

Posted: 12 Mar 2011 06:06
by !k
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")
)

Re: Batch moving using text file

Posted: 12 Mar 2011 06:38
by ni.va
Thanks,

but %%h may be file path with extension,

So both codes will result ...\1.txt\1.txt.

Re: Batch moving using text file

Posted: 12 Mar 2011 07:22
by !k

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

Re: Batch moving using text file

Posted: 12 Mar 2011 07:28
by ni.va
Thanks, that's working correctly. :D :D

Updated first post, Correct me if I'm wrong...

Re: Batch moving using text file

Posted: 20 Jun 2012 21:27
by alperefe
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?

Re: Batch moving using text file

Posted: 20 Jun 2012 21:53
by foxidrive
Shows us a few lines of your list, and describe which segment is which.

Re: Batch moving using text file

Posted: 28 Jun 2012 02:11
by alperefe
nobody has any idea?

Re: Batch moving using text file

Posted: 28 Jun 2012 02:24
by Ed Dyreen
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"

Re: Batch moving using text file

Posted: 28 Jun 2012 02:50
by foxidrive
Or show us a few lines of your list so we can create something that will work, ya dodo!

Re: Batch moving using text file

Posted: 28 Jun 2012 03:51
by alperefe
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.