copy list of files in a specified location - windows

I need to go through a list to copy the specified files into another location.
This is the used .bat file:
for /f "delims=" %%i in ('C:\Users\Documents\test\data\list_1.4.txt') do copy "C:\Users\Documents\test\data\Golden\%%i" "C:\Users\Documents\test\data\data_1.4"
But this does not work.
Any help?
I tried this too
#echo off
set src=C:\Users\Documents\test\data\Golden
set dst=C:\Users\Documents\test\data\data_1.4
set file=C:\Users\Documents\est\data\list_1.4.txt
for /F "usebackq tokens=*" %%a in ("%file%") do xcopy "%src%\*%%~a*" "%dst%" /C /Q /H /R /K /Y 1>nul 2>nul
pause

Based on your comment, the file content is something like:
"C:\Users\Documents\test\data\Golden\file1.txt"
"C:\Users\Documents\test\data\Golden\file2.pdf"
"C:\Users\Documents\test\data\Golden\file3.exe"
If that is the case, then:
#echo off
set "dest=C:\Users\Documents\test\data\data_1.4"
set "file=C:\Users\Documents\est\data\list_1.4.txt"
for /f "usebackq delims=" %%i in ("%file%") do (
if exist "%%~fi" copy /Y "%%~fi" "%destination%"
)

Related

Windows Batch ask for fileformat then search files from list and copy to directory

I made batch that search in folder structure for file mama1.jpg but I need it to ask user for file extension (eg. png, cr2 etc.)
Is it possible?
SET destination=%CD%
for /f "delims=" %%a in ('dir /b /s ^| find "mama.jpg"') do (
if not exist "Skopiowane_zdjecia" md "Skopiowane_zdjecia"
xcopy /y "%%a" "%destination%/Skopiowane_zdjecia")
#echo off
set /p ext="Type file format you search for (eg: jpg, cr2, nef):
SET destination=%CD%
for /f "delims=" %%a in ('dir /b /s ^| find "mama.%ext%"') do (
if not exist "Skopiowane_zdjecia" md "Skopiowane_zdjecia"
xcopy /y "%%a" "%destination%/Skopiowane_zdjecia")

Batch script to move files that contain certain string

Hi guys just got the following code script running to search in a directory for files that contain the string "ABC" and move them to the directory at the end.
for /f "eol=: delims=" %%F in ('dir /b^|find "ABC"') do move /Y "%%F" "C:\DESTINATION_DIRECTORY"
Was wondering how to modify this to not have to be run from the input directory, i.e to add a SOURCE_DIRECTORY variable so I can run this script from elsewhere but have it parse thru the SOURCE_DIRECTORY.
Thanks for any help.
#echo off
set "root=c:\st"
pushd %root% && (
for %%# in ("*ABC*") do echo move /Y "%%~f#" "C:\DESTINATION_DIRECTORY"
)
popd
or
for /f "tokens=* delims=" %%# in ('dir /b "%root%\*ABC*"') do echo move /Y "%root%\%%~nx#" "C:\DESTINATION_DIRECTORY"
for recursive search:
for /f "tokens=* delims=" %%# in ('dir /b /s "%root%\*ABC*"') do echo move /Y "%%~f#" "C:\DESTINATION_DIRECTORY"

How to check out with Batch , if a folder contains a .txt file and then copy the folder into an other location?

I would like to make a script with Batch. I want from this script to check it out, If a folder contains a file, "list.txt", and if it is in the folder i want to make a copy in an other location. I wrote some lines of code but it's not working. Any ideas?
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:loop
for /f %%i in ('dir "C:\Users\ntosis\Desktop\Draft" /ad /o:d /s /b') do (
SET a=%%i
for /R %%a %%t in (*.txt) do if "%%~nxt"=="list.txt" SET p=%%~dpnxt
echo !p!
IF DEFINED %p% ( robocopy C:\Users\ntosis\Desktop\Draft\%a% C:\Users\ntosis\Desktop\Copied\%a% /MOVE /E )
)
echo Folder is empty or does not exist
timeout /t 15
goto loop
The problem in this moment is that the second loop does not the check right.
How about this:
for /f %%i in ('dir "C:\Users\ntosis\Desktop\Draft" /ad /o:d /s /b') do (
IF EXIST "%%i\list.txt" (
robocopy %%i C:\Users\ntosis\Desktop\Copied\ /MOVE /E
)
)
#echo off
setlocal enableextensions disabledelayedexpansion
set "source=C:\Users\ntosis\Desktop\Draft"
set "target=C:\Users\ntosis\Desktop\Copied"
for /l %%t in (0) do (
set "found="
for /d /r "%source%" %%a in (list.txt) do if exist "%%a" (
for %%b in ("%%~dpa.") do (
echo Found "%%a"
robocopy "%%~dpa." "%target%\%%~nxb" /move /e
)
set "found=1"
)
if not defined found (
echo folder is empty or does not exist
)
timeout /t 15
)

I'd like to delete a bunch of files in a folder that have a specific extension and do not match a specified string

I am new to command prompt scripting and batch files. I have a folder with the following:
file1.pdf
file1.tif
file1_cropped.tif
file1.txt
file2.pdf
file2.tif
file2_cropped.tif
file2.txt...
filen.pdf
filen.tif
filen_cropped.tif
filen.txt
I would like to delete all the tif files that do not have "_cropped" in the filename. I have seen a few solutions for deleting files that have a specified extension, or that match a specific string, but I am trying to combine the two.
Much thanks,
Marc.
for /f "delims=" %%a in ('dir /b /a-d *.tif^|find /v /i "_cropped"') do echo del "%%a"
should suit.
perhaps you'd want
pushd "target directoryname"
for /f "delim...
popd
to specify a directory other than your current to be processed.
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO DEL to DEL to actually delete the files.
From command line, being in target directory:
for /F "eol=: delims=" %a in ('dir /b *.tif ^| find /V "_cropped"') do #del "%a"
Where we have:
FOR /F ["options"] %variable IN ('command') DO command [command-parameters]
In batch file:
for /F "eol=: delims=" %%a in ('dir /b *.tif ^| find /V "_cropped"') do #del "%%a"
A sample where we use a more interactive approach with confirmation:
Use typically as:
the_bat_file.bat exclude_these tif
Where option one is string in file names to exclude and two is file extension.
#echo off
set pat=_cropped
set ext=tif
IF "%1"=="--help" (
echo Usage %0 [exclude] [extension]
echo Default exclude : %pat%
echo Default extension: %ext%
goto end
)
GOTO part1
:confirm
SET /P CONF=Continue y/N?
IF /I "%CONF%" NEQ "Y" GOTO end
goto %con%
:part1
setlocal
IF NOT "%1"=="" set pat=%1
IF NOT "%2"=="" set ext=%2
echo Pattern : %pat%
echo Extension: %ext%
echo.
set con=part2
goto confirm
:part2
echo.
echo Files to delete:
echo.
for /F "eol=: delims=" %%a in ('dir /b "*.%ext%" ^| find /V "%pat%"') do echo "%%a"
echo.
set con=part3
goto confirm
:part3
for /F "eol=: delims=" %%a in ('dir /b "*.%ext%" ^| find /V "%pat%"') do DEL "%%a"
:end
endlocal

How to record how much data is deleted in a batch file

I have the following batch file
#echo off
Del *.tmp /s /Q
Del *.temp /s /Q
Del Thumbs.db /s /Q
pause
I need it to record how much data it has deleted in the following formats
How much in data (KB)
How many files it has deleted
Does any one know a code I could use?
#echo info about deleted files:
#for /f "delims=" %%a in ('dir /s /-c /a-s *.tmp^| findstr /i "File(s) Directory"') do (
#echo %%a
)
#del *.tmp /s /Q
Try this:
#echo off
setLocal enableDelayedExpansion
set filePattern=*.temp
set totalSize=0
for /F "tokens=* usebackq" %%a IN (`dir /b ^| findstr /R .!filePattern!$`) DO (
set size=%%~za
set /a totalSize=!totalSize! + !size!
)
del !filePattern! /s /Q
set /a totalSize=!totalSize! / 1024
echo Total size deleted (!filePattern!): !totalSize! KB
pause
You may have to change the !filePattern! accordingly based on your desired files to be deleted. Besides that, you may have to place this batch script on the same directory as the files to be deleted.

Resources