Batch file to move files based on a list

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
HenkB
Posts: 2
Joined: 28 Feb 2013 04:44

Batch file to move files based on a list

#1 Post by HenkB » 28 Feb 2013 04:49

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to move files based on a list

#2 Post by foxidrive » 28 Feb 2013 05:09

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

HenkB
Posts: 2
Joined: 28 Feb 2013 04:44

Re: Batch file to move files based on a list

#3 Post by HenkB » 28 Feb 2013 05:16

Thank you very much!
It works! Changed it to copy to be safe;)

SOLVED!

modrzej
Posts: 3
Joined: 23 Jun 2016 03:06

Re: Batch file to move files based on a list

#4 Post by modrzej » 23 Jun 2016 03:15

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:

Code: Select all

file1.txt
file2.txt etc.

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to move files based on a list

#5 Post by foxidrive » 23 Jun 2016 12:34

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.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Batch file to move files based on a list

#6 Post by Squashman » 23 Jun 2016 13:38

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.

modrzej
Posts: 3
Joined: 23 Jun 2016 03:06

Re: Batch file to move files based on a list

#7 Post by modrzej » 24 Jun 2016 01:19

I forgot change

Code: Select all

do move
to

Code: Select all

do copy
in quote post.

But i found a solution.
First install xxcopy and later change script

Code: Select all

move
to

Code: Select all

xxcopy
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.

modrzej
Posts: 3
Joined: 23 Jun 2016 03:06

Re: Batch file to move files based on a list

#8 Post by modrzej » 24 Jun 2016 01:34

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.

Furin Hoang
Posts: 3
Joined: 24 Jun 2016 08:38

Re: Batch file to move files based on a list

#9 Post by Furin Hoang » 24 Jun 2016 09:04

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to move files based on a list

#10 Post by foxidrive » 24 Jun 2016 14:43

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.

Furin Hoang
Posts: 3
Joined: 24 Jun 2016 08:38

Re: Batch file to move files based on a list

#11 Post by Furin Hoang » 25 Jun 2016 19:35

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?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to move files based on a list

#12 Post by foxidrive » 26 Jun 2016 14:46

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.

Furin Hoang
Posts: 3
Joined: 24 Jun 2016 08:38

Re: Batch file to move files based on a list

#13 Post by Furin Hoang » 27 Jun 2016 07:59

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file to move files based on a list

#14 Post by foxidrive » 28 Jun 2016 01:35

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.

Post Reply