when it comes to creating a batch-file I would sadly call myself a newbie and therefore it's kind of difficult for me to achieve what I want on my own:
so here is how my codes look:
#ECHO OFF
for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
del C:\ReadyToExport\*.pdf*
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
PAUSE
and here is what the code exactly does
1- for /r "C:\source" %%f in (*) do copy /y "%%f" C:\ReadyToExport
this line copies all files from source to another target folder the good thing is that this command copies all the files inside other subfolders from source and paste them into the folder C:\ReadyToExport without any subfolders.
2- del C:\ReadyToExport\*.pdf*
this command filters all the .pdf files because they are unnecessary
3-
for /f "delims=" %%i in ('xcopy "C:\ReadyToExport" C:\import /EXCLUDE:LogFile.log /S /E /D') do (
echo %%i >> C:\LogFile.log)
this command basically copies all files from "C:\ReadyToExport" into C:\import and writes the name of the recorded file in LogFile.log then the copied files will be excluded when the script runs again because I don't want to copy the files again if I already copied them before
and here is what I want to achieve:
I want to copy only modified files from the folder "C:\ReadyToExport" to the target C:\import but keep in mind that
the files in the target folder "C:\import" will be deleted but since the names of the files that have already been copied are registered in LogFile.log the files will be excluded and not copied again.
in another word: files in target file do not exist anymore but their names are written in LogFile
so is there any way to copy only modified ones? even though they don't exist in the target folder anymore? but their names are in LogFile.txt? can the script somehow write the last modified date in LogFile.txt near the file name? and then compare the date from the source? so it copies only files that have been changed and ignore all files that didn't?
p.s: using Xcopy, robocopy, etc is not a problem
any answer will be appreciated.
thnx
your method does, in fact, works so thank you so much not the way the I want with exclude:logfile.log
but this definitely worked:
robocopy C:\ReadyToExport\ C:\import /M /E
since it copies only the files that hast the attribute archivable checked in the settings.
Related
I have a temp folder where files get loaded into and then systematically purged out. Job to clear out files failed and now I have a number of directories/files that need to be manually deleted. I have deletedirs.txt that contains the names of all the folders I need deleted. I have generated the following to delete the files and folders however the path is not always the same:
FOR /F %%i IN (C:\dirlist.txt) DO echo y| del "C:\Temp Files\*" & rmdir /s /q "C:\Temp Files\" %%i
The issue is as the temp files get loaded in they form their own subfolder(s) so I have the following structure:
C:\Temp Files\101\folder1
C:\Temp Files\101\folder2
C:\Temp Files\103\folder4
C:\Temp Files\455\folder3
deletedirs.txt contains the folder names (folder1, folder2, etc.)
My script is failing because its searching for "C:\Temp Files\folder1"
How can I get the script to find 'folder1' and remove it regardless of subfolder number (101, 103, etc.)?
#ECHO OFF
SETLOCAL
FOR /d %%a IN ("c:\temp\*") DO (
FOR /f "delims=" %%s IN (q48890405.txt) DO (
IF EXIST "%%a\%%s\." ECHO RD /S /Q "%%a\%%s"
)
)
GOTO :EOF
You would need to change the directoryname to suit your circumstances.
I used a file named q48890405.txt containing some dummy data for my testing.
The required RD commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO RD to RD to actually delete the directories.
This should assign the subdirectory names to %%a in turn, then for each subdirectory found, read the names from the file into %%s. Then see whether a directory named %%a\%%s exists, and delete it if it does exist.
Note that your del "C:\Temp Files\*" will simply delete all of the files in "C:\Temp Files" over and over again. It really only needs to be done once (if that's what you had intended to do).
Removing the directory disposes of all of the files it contained, provided none are protected in some way (read-only or by UAC).
I am very new to batch scripting.
I need to write a batch script to search for a specific file(usually .zip or .7z extension) located on network drive directory(containing multiple folder and sub-folders with space in name) and copy the same to my local drive.
Also I need to copy a zip file containing "elf" keyword which will also be located in the same directory where my file is present.
For example:
Search file: abc.zip
Network drive:\abc.com\test
So basically I need to search for my file abc.zip in the network directory(including sub-folders) and if found copy the same to my local drive(say c:\Temp) and also I need to copy *elf* file to the same local directory.
Thanks in Advance.
#echo off
rem Prepare environment
setlocal enableextensions
rem Configure paths
set "source=\abc.com\test"
set "target=c:\test"
rem Change drive/path to source of files
pushd "%source%"
rem Recurse folders searching adecuated files and when found, copy to target
for /f "tokens=*" %%f in ('dir /s /b "abc.*z*" ^| findstr /i /e /c:".zip" /c:".7z"') do (
copy /y "%%~ff" "%target%"
if exist "%%~dpf\*elf*.zip" copy /y "%%~dpf\*elf*.zip" "%target%"
)
rem Return to previous drive/path
popd
rem Clean
endlocal
This will search the source folder hierarchy for indicated filename and an aproximate match based on file extension (.*z* matchs .zip, .7z and more), filters file extensions to only accept the cases needed (.zip and .7z) and copy the required files to target folder.
Have you considered using the Extended Copy Utility (xcopy)? Syntax is as simple as
xcopy "<directoryToCopyFrom>" "<directoryToCopyTo>" /C /S /D /Y /I
This will work assuming you're wanting to write a Windows batch script.
Searching for the "elf" string will be a bit trickier though. You might consider doing that using Java, and then call the batch file from the Java program.
for /d /r "drive:\abc.com\test" %%A in (*) do (
if exist "%%~A\abc.zip" copy "%%~A\abc.zip" "C:\Temp"
if exist "%%~A\*elf*" copy "%%~A\*elf*" "C:\Temp"
)
I'm looking to run a batch file in a specific folder and copy all files in that directory and rename those copies including it's extension to .txt
I.e, if there are files called fhnbye.zip and 212obtr.xls I want it to make copies of it and rename it to fhnbye.zip.txt and 212obtr.xls.txt
Is that possible?
This is a plain batch task:
#echo off
pushd "c:\data\folder"
echo creating "%%a.txt"
for /f "delims=" %%a in ('dir /b /a-d ') do copy /y "%%a" "%%a.txt" >nul
popd
OK, not to complicated, but there are some things you need to fill out before you can run this batch file (make sure its run in the same directory):
#echo off
set dir="C:\users\...[path to copy target (that is NEW location)]"
copy *.* %dir%\*.txt
And DONE!
Note, it will ask you for permission to overwrite files, if you have more then one file with the same name.
Edit 1
This code uses forfiles and will only work on windows 7 if you have windows tool installed (most computers will)
#echo off
set dir="C:\users\...[path to copy target (that is NEW location)]"
forfiles /c "cmd /c (copy #file %dir%\#file.txt)"
It should work fine.
Mona
I'm trying to tidy up a data folder and have written a batch file to take care of a lot of preliminary work (delete empty folders, delete junk files, etc), but I'm falling over when trying to deal with files within duplicate folders.
This is an example of the current situation:
w:\Data\Corporations\555\20130101\Concat_000001\555_20130101_data.zip
w:\Data\Corporations\555\20130101\Concat_000002\555_20130101_data.zip
w:\Data\Corporations\555\20130101\Concat_000003\555_20130101_data.zip
w:\Data\Corporations\555\20130101\Concat_000004\555_20130101_data.zip
There should only be one Concat folder per YYYYMMDD folder, and should look like this:
w:\Data\Corporations\555\20130101\Concat\555_20130101_data.zip
There are hundreds of folders in w:\Data\Corporations to be processed, so I figure I need to first of all find any folder named Concat_*, make a folder named Concat within the same parent folder, and then move any zip from Concat_ to Concat.
I have tried various combinations of FOR /D in (Concat_*) with MD and MOVE commands, but with no luck so far. I've also tried calling a subroutine from the FOR statement that would jump back a level in the tree, create a folder named Concat, go back to Concat_* and move the .zip files, but again with no luck.
Any help would be greatly appreciated.
Cheers,
Pete
try this:
for /r "w:\Data\Corporations\555" %%a in (*.zip) do for %%b in ("%%~dpa.") do md "%%~dpbConcat" 2>nul & move /y "%%~fa" "%%~dpbConcat"
The following does what you asked. If you uncomment the last line, then empty config_* folders will be removed. Non-empty config_* folders will be preserved.
#echo off
for /f "delims=" %%F in ('dir /b /ad /s concat_*') do (
if not exist "%%~dpFconcat\" mkdir "%%~dpFconcat\"
if exist "%%F\*.zip" move /y "%%F\*.zip" "%%~dpFconcat\" >nul
REM uncomment line below if you want to remove empty concat_* folders
REM dir /b "%%F"|findstr "^" >nul || rd "%%F"
)
I am trying to find a way to create a Windows batch script that will look at a target folder full of .pdf files and move (or copy) them to another directory with existing subfolders based on the filename.
The files and folders are names of actual people. I want to be able to get that person's pdf into their existing folder using a script.
Say I have 2 files in my folder; smithJohn015.pdf and thomasBill030.pdf.
I would like to be able to put smithJohn015.pdf into folder SmithJohn and thomasBill030.pdf into folder ThomasBill.
I don't want the script to create new folders or overwrite existing files if there's a duplicate filename.
I'm not necessarily looking for anyone to write a script for me, but if anyone can just get me started in the right direction it would be appreciated.
Try modifying this answer for your evil purposes.
#echo off
setlocal
pushd "c:\path\to\PDFs"
for /d %%I in (c:\Path\To\People\*) do (
for %%F in (*) do (
for /f %%A in ('echo %%~nF ^| find /i "%%~nI"') do (
set /p a="Moving %%F to %%I... "<NUL
move "%%F" "%%I" >NUL
echo Done.
)
)
)
popd
You'll need to add a check for if not exist pdffile before the move, but there's a starting direction for you anyway.
The following assumes the target subfolders' location contains only the subfolders where the PDFs may go and that every PDF that you want to move has a name formatted as the corresponding subfolder's name followed by exactly three characters (same as in your examples):
#ECHO OFF
FOR /D %%D IN ("D:\path\to\subfolders\*") DO (
MOVE "D:\path\to\PDFs\%%~nD???.pdf" "%%D"
)
Or as a one-liner to execute directly at the command prompt:
FOR /D %D IN ("D:\path\to\subfolders\*") DO (MOVE "D:\path\to\PDFs\%~nD???.pdf" "%D")
folderlist.txt contains all names of folders in which you want to copy respective PDFs.
xcopy is copy command. format xcopy "source" "destination" /parameters.
pause is just to keep command window on
for /F "tokens=*" %%A in (folderlist.txt) do (
xcopy "E:\path\to\source folder\<prefix>%%A<suffix>.pdf" "E:\path\to\destination folder\<prefix>%%A<suffix>\" /s)
pause
You can use wildcards in source & destination paths.