i have list of folders
files in all the folder are named in the same way
I want copying them in one folder without losing the order (folder 01(file01-02...) to folder 10)
I didn't find the way to do it with cmd or another way because I want to do it without any software just with windows
try this:
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "startfolder=c:\data"
SET "targetfolder=x:\data"
for /d /r "%startfolder%" %%a in (*) do (
SET "fname=%%~a"
SET "fname=!fname:%startfolder%=!"
ECHO MD "%targetfolder%!fname!\%%~nxa" 2>nul
for %%b in ("%%~fa\*") do ECHO COPY "%%~fb" "%targetfolder%!fname!\%%~nb-new name%%~xb"
)
Look at the output and remove the word echobefore MDand COPY if it looks good.
Related
I have a relatively simple task to do. I have to move 100 files which are located in various different folders, into one single folder. So far I have the script that will do this if i am moving the files from one folder to another folder based on the list of files.
#echo off
set Source=C:\IMAGES_SOURCE
set Target=C:\IMAGE_DESTINATION
set FileList=C:\ImageList.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 copy "%Source%\%%a"
"%Target%"
:Exit
echo.
echo press the Space Bar to close this window.
pause > nul
So for this instance, I created the list of the items and scrip will look for the file name in source location and once find them it will move them to a destination location.
I need to adjust this so it reads the names of the files and try to find them in the list of the folders that they might be.
What i have tried is to change the set Source to be also separate list that will contain posible folder names:
set Source=C:\DirList.txt
But when i do this it does not copy the images.
Any idea how to do this?
First thing that comes to mind is dir /S meaning it will search using the dir command. This is untested, but you'll get the idea:
#echo off
cd /d C:\Images_source
setlocal enabledelayedexpansion
for /F "delims=" %%i in (C:\Imagelist.txt) do (
set var="%%i"
for /F "delims=" %%a in ('dir /B /S !var!') do echo copy "%%~fa" C:\Image_Destination
)
For more on the commands I used, try help by running from from commandline for /? and dir /?
Once confident that it will copy only what you want to copy/move, then remove the echo before copy.
I have a list of filenames without their extension and a folder and sub-folders of those files with their extensions. I am trying to use the list to copy those files to a different location. I tried to use a script I found here and modified it:
#echo off
FOR /R "P:\Case\MyCase\Productions" %%G in (.) do (
for /F "delims=" %%i in (UniqueFileList.txt) do (
if exist %%G\%%i.* xcopy %%G\%%i.* "C:\Temp\CopiedFiles" /D /Y
)
)
My file names are TIN00001.msg, TIN00002.txt, TIN00003.jpg, etc...
and the names in my file list is TIN00001, TIN00002, TIN00003, etc...
How can I use a script to copy the files ?
Any help is appreciated.
Thanks !!
Mustu
I'm not sure if I fully understand your intention but my best guess is that something like this would suit you.
#Echo Off
Set "rootDir=P:\Case\MyCase\Productions"
Set "destDir=C:\Temp\CopiedFiles"
Set "listTxt=%~dp0UniqueFileList.txt"
For /F "Delims=" %%A In ('Where/R "%rootDir%" *.*^|FindStr/LIG:"%listTxt%"'
) Do FindStr/LIX "%%~nA" "%listTxt%">Nul 2>&1 && XCopy "%%A" "%dstDir%" /D /Y
If %listTxt% isn't in the same location as the script then change it's location to a full path too.
I want to copy all files on a list to another location and create sub-folders in that location based on certain attributes of the filenames. In this case, the company names. All files are currently located in desktop\main.
For example, lets say the list has filenames like
From((Sales#JonesCompany.com))main.txt
From((AR#PeterIndustries.com))main.txt
From((AP#BaseCorporation.com))main.txt
The script should copy those files, make a directory like JonesCompany.com, and finally copy all respective filenames to that location.
The end result should be like
desktop\final_location\JonesCompany\
desktop\final_location\PeterIndustries\
desktop\final_location\BaseCorporation\
So far I have
for /f "delims=" %%i in (main.txt) do echo F|xcopy "C:\Users\Desktop\Main\%%i" MD %%i "C:\Users\Desktop\final_location\%%i" /i /z /y
Thanks in advance
I think this does what you want:
#ECHO OFF
SETLOCAL
set "basepath=C:\Users\Desktop\Main\"
set "filter=*.txt"
set "targetpath=C:\Users\Desktop\final_location\"
echo scanning %basepath%%filter%
for %%i in ("%basepath%%filter%") do (
for /F "tokens=1,2,3 delims=#)" %%a in ("%%i") do (
echo Creating Folder %%b...
mkdir %basepath%%%b\
echo copying file %%i to %targetpath%%%b\...
copy "%%i" %targetpath%%%b\
)
)
You can easily change the basepath, targetpath and filter variables as needed.
Explanation
The trickiest part is for /F "tokens=1,2,3 delims=#)" %%a in ("%%i") do (. %%i is a filename. Let's say it's From((Sales#JonesCompany.com))main.txt. This for statement will split the string at any instance of the delimiters # or ). This leaves us with the following array of strings:
%%a: "From((Sales"
%%b: "JonesCompany.com"
%%c: NULL (I think)
%%d: "main.txt"
Obviously, we want %%b so we use it as the new folder name. We then copy %%i, the original full path, to the new folder.
update removed a loop thanks to #Stephan pointing out something I didn't realize!
I have a backup program that saves .bak files into a folder which it rotates on a weekly basis automatically.
The files are given given names like this:
DB_Live_19052015.bak
DB_Test_19052015.bak
DB_Live_18052015.bak
DB_Test_18052015.bak
The backup program doesn't allow me to edit these names and I actually don't want it to.
What I do need is to be able to copy the newest file of each set DB_Live_XXXXXXXX.bak & DB_Test_XXXXXXXX.bak and rename them to drop the date so I end up with files like this for DR:
dr/DB_Live.bak
dr/DB_Test.bak
This would be overwritten each time the script was run.
No I can copy the latest file in a folder and rename it using scripting but I cannot get my head round how to
A. get the set of latest files (multiple)
B. rename these files based on their original names to drop only the on the end.
What I am expecting to have to do is the following:
copy the latest files to a dr folder
get the file names for each file
rename the files and overwrite anything already there with that name
I'm going to be adding this scripting into the backup program so it runs when the backup has finished.
The reason for these files is so I can RSYNC them off site without sending the whole file every time.
#echo off
setlocal enableextensions disabledelayedexpansion
set "source=%cd%\source"
set "target=%cd%\target"
for %%a in (DB_Live DB_Test) do (
set "first=1"
for /f "delims=" %%b in ('
dir /a-d /tw /o-d /b "%source%\%%a_*.bak"
') do if defined first (
set "first="
copy /b /y "%source%\%%~b" "%target%\%%a%%~xb"
)
)
For each set of files, execute a dir command in reverse modified date order. In this list the first file is the last modified. Copy this file to the target overwritting the existing file (if present).
#echo off
echo.>%temp%\tempone.txt
if not exist dr md dr
if not exist dblive md dblive
if not exist dbtest md dbtest
setlocal enabledelayedexpansion
for %%I in (*) do (
set red=%%I
set blue=!red:~0,7!
if "!blue!"=="DB_Live" copy /y !red! dblive>nul
if "!blue!"=="DB_Test" copy /y !red! dbtest>nul
)
pushd %cd%
cd dblive
for %%I in (*) do (
set green=%%I
set green=!green:DB_Live_=!
set green=!green:.bak=!
echo !green! >>%temp%\tempone.txt
)
set max=0
for /f %%x in (%temp%\tempone.txt) do (
set "FN=%%~nx"
if !FN! GTR !max! set max=!FN!
)
echo the latest dblive file is DB_Live_!max!.bak--copied to dr folder
set dblatest=DB_Live_!max!.bak
copy /y !dblatest! %temp%>nul
popd
copy /y %temp%\!dblatest! dr>nul
::same for dbtest
pushd %cd%
cd dbtest
for %%I in (*) do (
set green=%%I
set green=!green:DB_Test_=!
set green=!green:.bak=!
echo !green! >>%temp%\temptwo.txt
)
set max=0
for /f %%x in (%temp%\temptwo.txt) do (
set "FN=%%~nx"
if !FN! GTR !max! set max=!FN!
)
echo the latest dbtest file is DB_Test_!max!.bak--copied to dr folder
set dblatest=DB_Test_!max!.bak
copy /y !dblatest! %temp%>nul
popd
copy /y %temp%\!dblatest! dr>nul
::rename both files
pushd %cd%
cd dr
ren DB_Live_* DB_Live.bak
if %errorlevel%==0 echo renamed dblive
ren DB_Test_* DB_Test.bak
if %errorlevel%==0 echo renamed dbtest
pause
put in the same folder as the .bak files. hope this helps!
I've seen some scripts examples over SO, but none of them seems to provide examples of how to read filenames from a .txt list.
This example is good, so as to copy all files from A to B folder
xcopy c:\olddir\*.java c:\newdir /D /E /Q /Y
But I need something like the next, where I can fill actually the source and destination folder:
#echo off
set src_folder = c:\whatever\*.*
set dst_folder = c:\foo
xcopy /S/E/U %src_folder% %dst_folder%
And instead of src_folder = c:\whatever\*.*, those *.* need to be list of files read from a txt file.
File-list.txt (example)
file1.pds
filex.pbd
blah1.xls
Could someone suggest me how to do it?
Given your list of file names in a file called File-list.txt, the following lines should do what you want:
#echo off
set src_folder=c:\whatever
set dst_folder=c:\target
for /f "tokens=*" %%i in (File-list.txt) DO (
xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)
I just tried to use Frank Bollack and sparrowt's answer, but without success because it included a /U switch for xcopy. It's my understanding that /U means that the files will only be copied if they already exist in the destination which wasn't the case for me and doesn't appear to be the case for the original questioner. It may have meant to have been a /V for verify, which would make more sense.
Removing the /U switch fixed the problem.
#echo off
set src_folder=c:\whatever
set dst_folder=c:\target
for /f "tokens=*" %%i in (File-list.txt) DO (
xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)
This will do it:
#echo off
set src_folder=c:\batch
set dst_folder=c:\batch\destination
set file_list=c:\batch\file_list.txt
if not exist "%dst_folder%" mkdir "%dst_folder%"
for /f "delims=" %%f in (%file_list%) do (
xcopy "%src_folder%\%%f" "%dst_folder%\"
)
The following will copy files from a list and preserve the directory structure. Useful when you need to compress files which have been changed in a range of Git/SVN commits¹, for example. It will also deal with spaces in the directory/file names, and works with both relative and absolute paths:
(based on this question: How to expand two local variables inside a for loop in a batch file)
#echo off
setlocal enabledelayedexpansion
set "source=input dir"
set "target=output dir"
for /f "tokens=* usebackq" %%A in ("file_list.txt") do (
set "FILE=%%A"
set "dest_file_full=%target%\!FILE:%source%=!"
set "dest_file_filename=%%~nxA"
call set "dest_file_dir=%%dest_file_full:!dest_file_filename!=%%"
if not exist "!dest_file_dir!" (
md "!dest_file_dir!"
)
set "source_file_full=%source%\!FILE:%source%=!"
copy "!source_file_full!" "!dest_file_dir!"
)
pause
Note that if your file list has absolute paths, you must set source as an absolute path as well.
[¹] if using Git, see: Export only modified and added files with folder structure in Git
This will also keep the files original file directory:
#echo off
set src_folder=c:\whatever
set dst_folder=c:\target
set file_list=C:\file_list.txt
for /f "tokens=*" %%i in (%file_list%) DO (
echo f | xcopy /E /C /R /Y "%src_folder%\%%i" "%dst_folder%\%%i"
)
Also can use robocopy and Not use for loop with xcopy - can parse list of files in argument.
robocopy Source_folder Destination_folder [files_to_copy] [options]
Files to copy it's string with Space delimiter.
For example:
robocopy . "d:\my folder" *.txt "my file one.cpp" file2.cpp
robocopy "d:\F 15" "d:\backup\F 15" /E