Batch copy files from list of files in subfolders - file

I am attempting to copy specific files from a list, "filelist.txt" to a destination folder. With the code presented below, I can only do this from a specific source folder and have only the files names in the text file (as compared to the full path). I wanted to copy files from subfolders in the main folder. How can I do this if I already have the full path of the files that I need copied in the text file?
Here is the start of the code that I have (built from the code presented here):
CODE
#ECHO ON
SET FileList=G:\filelist.txt
SET Source=G:\fold1
SET Destination=G:\Copy1
FOR /F "USEBACKQ TOKENS=*" %%F IN ("%FileList%") DO XCOPY /F /Y "%Source%\%%~F" "%Destination%\"
GOTO :EOF

It worked on my condition
#echo off
set filelist=filelist.txt
set source=StackOverFlow
set destination=copyl
:work
for %%i in (%filelist%) do set filename=%%~nxi
copy /y %filelist% %tmp%
set "filelist=%tmp%\%filename%"
set "filel=%tmp%\file"
:data
set data=con
set /p data=<%filelist%
more +1 "%filelist%" > "%filel%"
del /q /f %filelist%
ren %filel% %filename%
if not exist "%Source%\%data%" goto exitx
copy "%Source%\%Data%" "%Destination%"
goto data
:exitx
del /q /f %filelist%
del /q /f %filel%
cls
echo Operation Complete!
pause
exit
Example of filelist.txt
test.txt
text.bat
test.cmd
biltudas1.md

Related

Copy a file from one directory and replace the file in multiple directory

I need to copy a jar file from directory(source) and replace the file in the destination. But the problem is my destination directories are different as explained below:
Source=D:\temp\R56A
Target=D:\path\AP\Different_folders\lib\i2
Target folder example:-
D:\path\AP\ABC1\lib\i2
D:\path\AP\XY_C\lib\i2
D:\path\AP\GHS3\lib\i2
I AM NOT ABLE TO FETCH THROUGH DIFFERENT FOLDER NAMES and the script not taking it.
This is for a windows box. Can we copy the folder name in a text file and call that text file as variable in a for loop? Is it possible?
#ECHO OFF
REM SETLOCAL ENABLEDELAYEDEXPANSION
set Source=D:\temp\R56A
set Target=D:\path\AP\<Different_Directory_names>\lib\i2
set file=i2-bam.jar
for /f "delims=" %%f in ('dir /a-d /b /s "%Source%\%file%"') do (
copy /V "%%f" "%Target%\" 2>nul
)
PART 2
#ECHO OFF
for /d "D:\temp\R56A\" %%f in (i2-bam.jar) do copy %%f "D:\path\AP\<Different_Directory_names>\lib\i2"
Is this what you're trying to do?
#Echo Off
Set "Source=D:\temp\R56A"
Set "File=i2-bam.jar"
Set "Target=D:\path\AP"
Set "Sub=lib\i2"
If Not Exist "%Source%\%File%" Exit /B
If Not Exist "%Target%\" Exit /B
For /D %%A In ("%Target%\*")Do If Exist "%%A\%Sub%\" Copy /Y "%Source%\%File%" "%%A\%Sub%">Nul

Move files to parent directory if their extension matches the sub-directory extension

I am looking for a batch script which will move files from a sub-directory to its parent if their extension matches the sub-directory extension.
Examples:
Move any .txt file from directory parent\files.txt\
"parent\files.txt\test.txt" will become "parent\test.txt"
Move any .zip file from directory parent\files.zip\
"parent\files.zip\test.zip" will become "parent\test.zip"
I want only to move the file if its extension is the same as that of its sub-directory name. The sub-directory and any other content has to be left alone.
This is what I have now, but it only removes my files:
#echo off
md temp
set view=
if "%view%"=="1" #echo on
color 72
mode con cols=30 lines=8
setlocal enableDelayedExpansion
set /p location_with_dirs=location:
echo:
type nul> ".\temp\folderlist.txt"
FOR /D %%G IN ("%location_with_dirs%\*") DO (
echo process file:
echo %%G
choice
if "%errorlevel%"=="1" echo %%G >> ".\temp\folderlist.txt"
cls
)
FOR /f "delims=" %%G in (".\temp\folderlist.txt") DO (
for %%F in (%%G\*.*) do move /Y %%F "%location_with_dirs%\"
rd %%G
)
del /f /q ".\temp\folderlist\*.txt"
I'm sure there will be many ways of achieving this, here's one:
#CD /D "Parent" 2>Nul || Exit /B
#For /D %%A In (*) Do #If /I Not "%%~xA"=="" Move /-Y "%%A\*%%~xA">Nul
Just change Parent on line 1 to the full or relative path of your parent directory.
Just as a courtesy, heres a version which hopefully improves on the more important part of your provided code, (it ignores the progress bar and color stuff):
#Echo Off
:AskDir
Set "dir_with_files="
Set /P "dir_with_files=location: "
If "%dir_with_files%"=="" GoTo AskDir
If "%dir_with_files:~-1%"=="\" Set "dir_with_files=%dir_with_files:~,-1%"
If Not Exist "%dir_with_files%\" GoTo :AskDir
Set "_rand=%dir_with_files%\%random%.organizer.lock.txt"
Type Nul>"%_rand%" 2>Nul && (Del "%_rand%") || GoTo :AskDir
If "%dir_with_files:~-1%"==":" Set "dir_with_files=%dir_with_files%\"
CD /D "%dir_with_files%" 2>Nul || Exit /B
For /D %%A In (*) Do If /I Not "%%~xA"=="" Move /-Y "%%A\*%%~xA">Nul 2>&1
Exit /B.

Batch script to copy two different file extension (.doc and .log) to newly created folder or directory (with date)

I'm new to scripting and I'm writing a batch file that will copy the recent or newly created files (.doc and .log) from two different source/directory and create a destination folder name "backup(date today)"
Basically , the two files are from different source and copy to one folder
New_doc.doc from C:\Doc_backup
New_log.log from C:\Log_backup
Destination: d:\file_backup\backup20140330
My code below will create a folder (with date) and copy the only file from source.
but I don't know copying two different file from different source to single folder.
#echo off
setlocal
::Create Directory with date
SET dd=%date:~0,2%
SET mm=%date:~3,2%
SET yy=%date:~6,4%
SET date=%yy%%mm%%dd%
md c:\file_backup\backup%date%
set srcDir=c:\doc_backup
set srcDir2=c:\log_backup
set destdir=c:\file_backup\backup%date%
set lastmod=
pushd %srcDir%
for /f "tokens=*" %%a in ('dir *.DOC /b /od 2^>NUL') do set lastmod=%%a
if "%lastmod%"=="" echo Could not locate files.&goto :eof
copy "%lastmod%" "%destDir%"
pause
All copy task could be performed, supposing variables srcDir and destDir are defined correctly, with next code snippet without for loop:
pushd "%destDir%"
copy /B "%srcDir%\*.doc"
popd
However, let's correct some mistakes in your code:
#echo off
setlocal
::Create Directory with date
SET "dd=%date:~0,2%"
SET "mm=%date:~3,2%"
SET "yy=%date:~6,4%"
SET "xdate=%yy%%mm%%dd%"
md "c:\file_backup\backup%xdate%"
set "srcDir=c:\doc_backup"
set "destdir=c:\log_backup\backup%xdate%"
md "%destdir%" 2>NUL
rem set lastmod=
pushd %srcDir%
for /f "tokens=*" %%a in ('dir *.DOC /b /od 2^>NUL') do (
rem set lastmod=%%a
rem if "%lastmod%"=="" echo Could not locate files.&goto :eof
copy /B "%%a" "%destDir%\"
)
pause
popd
endlocal

modify batch that copy in clipbrd older filename .mxf in folderA that are not in folderB

Thanks to foxidrive I have this code:
batch to copy in clipbrd filenameandpath of the older file in folderA except the same name in folderB
#echo off
:loop
set "d="
set "done="
set /p "d=Type source path (or press enter for current folder): "
if not defined d set "d=%cd%"
if not exist "%d%" echo Enter valid path - try again & goto :loop
cd /d "%d%"
for /f "delims=" %%a in ('dir *.mxf /b /od /a-d') do (
if defined done goto :EOF
if not exist "d:\folderB\%%~na.*" (
echo %%~fa|clip >nul
set done=1
)
)
OK, it work very well, thanks!
now I should like to do this:
and then the batch have to nename a file o:\temp.avi with the filename choosed by the batch (and putted into the clipbrd) example: if the batch have choose (into folderA) C0001.mxf, it have to rename o:\temp.avi --------> C0001.avi
Add the line in the middle between the other two.
echo %%~fa|clip >nul
ren "o:\temp.avi" "%%~nxa.avi"
set done=1
Read the help section on how Stack Overflow works: https://stackoverflow.com/tour

How to copy files from a random folder into a destination folder in batch?

I need a batch script to copy files from a random subfolder of a specific directory into a destination directory.
For example, there will be a number of files in their own subdirectory, for example
.../source/a/file.txt
.../source/b/file.txt
So there a number of these files, and I would like to randomly select one of them and have it copied into the new directory
.../destination/file.txt
So the file.txt in the destination is just being overwritten with other files that have the same name but different content.
I'm new to batch scripting and I can't quite figure out how to select each file from a random subfolder. I'd also like it to repeat every 30 seconds until I terminate the script, but I think it should be easy enough to just make a second script that calls this .bat file every 30 seconds once I get it going.
Thanks!
This can do what you request. Just set your source directory, destination directory, and your file name filter.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd "...\source\"
:: Enumerate Files. Method 1
set "xCount=0"
for /r %%A in (file.txt) do if exist "%%~A" set /a "xCount+=1"
echo %xCount%
:: Select a Random File.
set /a "xIndex=%Random% %% %xCount%"
echo %xIndex%
:: Find an Copy that File. Method 1
set "xTally=0"
for /r %%A in (file.txt) do if exist "%%~A" (
if "!xTally!" EQU "%xIndex%" (
xcopy "%%~fA" "...\destination\file.txt" /Y
goto End
)
set /a "xTally+=1"
)
:End
popd
endlocal
pause
Type xcopy /? to see all of its options.
Here are some alternate loop methodologies for the file enumeration.
:: Enumerate Files. Method 2
set "xCount=0"
for /f %%A in ('dir *.txt /a:-d /s ^| find "File(s)"') do set "xCount=%%~A"
echo %xCount%
:: Find an Copy that File. Method 2
set "xTally=0"
for /f "delims=" %%A in ('dir *.txt /a:-d /b /s') do (
if "!xTally!" EQU "%xIndex%" (
xcopy "%%~fA" "...\destination\file.txt" /Y
goto End
)
set /a "xTally+=1"
)
Enjoy :)
#echo off
setlocal EnableDelayedExpansion
rem Enter into the directory that contain the folders
pushd \Fullpath\source
rem Create an array with all folders
set i=0
for /D %%a in (*) do (
set /A i+=1
set folder[!i!]=%%a
)
rem Randomly select one folder
set /A index=(%random%*i)/32768 + 1
rem Copy the desired file
copy "!folder[%index%]!\file.txt" "\Fullpath\destination" /Y
rem And return to original directory
popd
The features of batch script is
It copies all files from source to destination folder with similar structure(even retains empty folders).
Can retain N number of days recent files in Archive folder(source) remaining files will be moved to backup folder(destination).
Can be scheduled to N number of days to N number of years.
Can be used in any Source to destination folder backup.
Source folder can add N number of folder any time and delete folder or files, but Destination folder always adds the folder and never deletes any folder or file.
#echo off
Set "sourcefolder=E:\Interfaces"
Set "destinationfolder=E:\BackupInterface"
If Exist %sourcefolder% (
For /F %%* In ('Dir /b /aD "%sourcefolder%" 2^>nul') do (If Not Exist "%destinationfolder%\%%*" ( RD /S /Q "%destinationfolder%\%%*")
xcopy /e /v /i /y /q "%sourcefolder%\%%*" "%destinationfolder%\%%*"
forfiles /p "%sourcefolder%\%%*" /s /d -30 /c "cmd /c del /Q /S #file" )
) Else (echo.Source folder could not be found)
:end of batch
echo.&echo.finished!

Resources