Page 1 of 1
Batch file to move files based on a list
Posted: 28 Feb 2013 04:49
by HenkB
Hello,
I am new on this forum, I need help with a problem:
I have a folder with all kind of files and I have a list of some of these files.
Now I need to copy/move the files that are in the list to another directory.
Really would appriciate it, stuck on this for time already..
I have this:
Code: Select all
@echo off
set Source=C:\Users\MAs\Desktop\moving\PI
set Target=C:\Users\MAs\Desktop\moving\no
set FileList=C:\Users\MAs\Desktop\moving\not.txt
echo.
if not exist "%Source%" echo Source folder "%Source%" not found & goto Exit
if not exist "%FileList%" echo File list "%FileList%" not found & goto Exit
if not exist "%Target%" md "%Target%"
for /F "delims=" %%a in ('type "%FileList%"') do echo move "%Source%\%%a"
%Target%"
:Exit
echo.
echo press the Space Bar to close this window.
pause > nul
but this gives me an error:
'C:\Users\MAs\Desktop\moving\no"' is not recognized as an internal or external command, operable program of batch file
cheers
Re: Batch file to move files based on a list
Posted: 28 Feb 2013 05:09
by foxidrive
Try this:
Code: Select all
@echo off
set Source=C:\Users\MAs\Desktop\moving\PI
set Target=C:\Users\MAs\Desktop\moving\no
set FileList=C:\Users\MAs\Desktop\moving\not.txt
echo.
if not exist "%Source%" echo Source folder "%Source%" not found & goto Exit
if not exist "%FileList%" echo File list "%FileList%" not found & goto Exit
if not exist "%Target%" md "%Target%"
for /F "delims=" %%a in ('type "%FileList%"') do move "%Source%\%%a" "%Target%"
:Exit
echo.
echo press the Space Bar to close this window.
pause > nul
Re: Batch file to move files based on a list
Posted: 28 Feb 2013 05:16
by HenkB
Thank you very much!
It works! Changed it to copy to be safe;)
SOLVED!
Re: Batch file to move files based on a list
Posted: 23 Jun 2016 03:15
by modrzej
foxidrive wrote:Try this:
Code: Select all
@echo off
set Source=C:\Users\MAs\Desktop\moving\PI
set Target=C:\Users\MAs\Desktop\moving\no
set FileList=C:\Users\MAs\Desktop\moving\not.txt
echo.
if not exist "%Source%" echo Source folder "%Source%" not found & goto Exit
if not exist "%FileList%" echo File list "%FileList%" not found & goto Exit
if not exist "%Target%" md "%Target%"
for /F "delims=" %%a in ('type "%FileList%"') do move "%Source%\%%a" "%Target%"
:Exit
echo.
echo press the Space Bar to close this window.
pause > nul
I have use this script to copy files from source to destination folder, it works great. But i don't know how to rebuild code to make a copy of file without overwrite?
I want to make a duplicate of source file in a destination folder with rename or something.
ex.:
In source folder i have:
In list I have:
Code: Select all
file1.txt
file2.txt
file1.txt
file2.txt
In destination folder i wan't to have
Code: Select all
file1.txt
file2.txt
file1(1).txt
file2(1).txt
I'll try to add some countenrs from another examples from web, but it doesn't work

My knowledge about scripting is poor

Please help

Re: Batch file to move files based on a list
Posted: 23 Jun 2016 12:34
by foxidrive
The actual script you're using can change the code that is used, and it would be a waste of time to write code without knowing the task.
Re: Batch file to move files based on a list
Posted: 23 Jun 2016 13:38
by Squashman
Well the current script is moving, not copying.
I am not understanding how you have one source folder but the same file names twice in your list.
Re: Batch file to move files based on a list
Posted: 24 Jun 2016 01:19
by modrzej
I forgot change
to
in quote post.
But i found a solution.
First install xxcopy and later change script
to
with /SG switch.
Code: Select all
@echo off
set Source=E:\test1
set Target=E:\test2
set FileList=E:\lista.txt
echo.
if not exist "%Source%" echo Folder zrodlowy "%Source%" nie istnieje & goto Exit
if not exist "%FileList%" echo Plik z lista "%FileList%" nie istnieje & goto Exit
if not exist "%Target%" md "%Target%" & echo Folder docelowy "%Target%" zostal utworzony
for /F "delims=" %%a in ('type "%FileList%"') do xxcopy "%Source%\%%a" "%Target%" /SGN
echo Kopiowanie plikow z listy zakonczone powodzeniem.
:Exit
echo.
echo Nacisnij dowolny klawisz aby zakonczyc.
pause > nul
Works fine for me.
Re: Batch file to move files based on a list
Posted: 24 Jun 2016 01:34
by modrzej
Squashman wrote:Well the current script is moving, not copying.
I am not understanding how you have one source folder but the same file names twice in your list.
OK. Let me explain.
Source folder is a base of graphics.
Target folder is a production base for printer.
On list, we have graphics which we need to print. But for example we need to pritn test1.jpg twice, so i have to put it into list twice.
Now i have to copy from base to production test1.jpg twice without overwrite - i need to add something to name of file.
Re: Batch file to move files based on a list
Posted: 24 Jun 2016 09:04
by Furin Hoang
foxidrive wrote:Try this:
Code: Select all
@echo off
set Source=C:\Users\MAs\Desktop\moving\PI
set Target=C:\Users\MAs\Desktop\moving\no
set FileList=C:\Users\MAs\Desktop\moving\not.txt
echo.
if not exist "%Source%" echo Source folder "%Source%" not found & goto Exit
if not exist "%FileList%" echo File list "%FileList%" not found & goto Exit
if not exist "%Target%" md "%Target%"
for /F "delims=" %%a in ('type "%FileList%"') do move "%Source%\%%a" "%Target%"
:Exit
echo.
echo press the Space Bar to close this window.
pause > nul
Thank you so much. This helps me save a lot of time

. Does it work with multiple source folders? if it does, pls kindly guide me, thks in adv
Re: Batch file to move files based on a list
Posted: 24 Jun 2016 14:43
by foxidrive
Furin Hoang wrote:Does it work with multiple source folders? if it does, pls kindly guide me, thks in adv
If there are files in your list that exist in different folders, then it can be done if the list contains the fully qualified paths.
Explain your task so we understand what you want to do.
Re: Batch file to move files based on a list
Posted: 25 Jun 2016 19:35
by Furin Hoang
foxidrive wrote:If there are files in your list that exist in different folders, then it can be done if the list contains the fully qualified paths.
Explain your task so we understand what you want to do.
You're right, my files in list exist in different folders, I want to copy and move them from a source folder which contains multiple folders to my target folder. In addition, is this possible if source folder exists in another computer?
Re: Batch file to move files based on a list
Posted: 26 Jun 2016 14:46
by foxidrive
Furin Hoang wrote:foxidrive wrote:If there are files in your list that exist in different folders, then it can be done if the list contains the fully qualified paths.
Explain your task so we understand what you want to do.
You're right, my files in list exist in different folders, I want to copy and move them from a source folder which contains multiple folders to my target folder. In addition, is this possible if source folder exists in another computer?
What is inside your list of files?
That is the important part for use to know.
They can be moved or copied to another computer if you have access rights to that machine, and it's a local machine in your LAN.
Creating the folder if it doesn't exist is easy too, if that is what you mean.
Re: Batch file to move files based on a list
Posted: 27 Jun 2016 07:59
by Furin Hoang
foxidrive wrote:That is the important part for use to know.
They can be moved or copied to another computer if you have access rights to that machine, and it's a local machine in your LAN.
Creating the folder if it doesn't exist is easy too, if that is what you mean.
For example, my source folder contains multiple folders named A, B,C, ...
sub folder "A" contains file1.pdf, file2.pdf, file3.pdf
sub folder "B" contains file4.pdf, file5.pdf, file6.pdf
sub folder "C" contains file7.pdf, file8.pdf, file9.pdf
.....
And I have a list name of files I want to copy to target folder that contains file2.pdf, file6.pdf, file7.pdf,...
Is this clear enough for you to understand what I mean? I'm really appreciate your help
Re: Batch file to move files based on a list
Posted: 28 Jun 2016 01:35
by foxidrive
Furin Hoang wrote:For example, my source folder contains multiple folders named A, B,C, ...
sub folder "A" contains file1.pdf, file2.pdf, file3.pdf
sub folder "B" contains file4.pdf, file5.pdf, file6.pdf
sub folder "C" contains file7.pdf, file8.pdf, file9.pdf
.....
And I have a list name of files I want to copy to target folder that contains file2.pdf, file6.pdf, file7.pdf,...
It's not accurate information but I can see that your four files do not have the folder names added to them, so the batch script has no way of knowing which folders they are all in.