Folder and subfolder copy at MSDOS - batch-file

I have a folder with 9500 documents inside(let's call if folder1). I want to make a copy of this folder with the structure it have but i want in the new folder(let's call it folder2) all the documents to be blank. Folder1 contains several type of documents. Untill now i have as code :
#echo off
set "source=C:\Users\DM\Desktop\folder1\*.*"
set "dest=C:\Users\DM\Desktop\folder2"
xcopy "%source%" "%dest%" /t/e
for /f "tokens=2 delims=>" %%a in ('xcopy "%source%" "%dest%" /s/h/e/k/f/c/l') do (
for /f "tokens=*" %%b in ("%%a") do type nul >"%%b"
)
pause
The bat goes ok, but after a while there are 3 different errors on the cmd and it stacks there (my windows are greek so i'm trying to translate the errors). One is that "memory is not enough" , the next one is "The system cannot locate the disk(or drive) path specified" (it's one of the two drive/disk i think.) and the last one is "File name syntax, directory or volume label is incorect". It stops at a specific htm file that has "<>" characters on the title.
Can you please tell me what should i add?
Thanks in advance

I hope the code below will help. But it echo the new line to all files. And I couldn't use source as parameter in !fileRelPath:C:\te mp\=!
#echo off
setlocal enabledelayedexpansion
set source=C:\te mp\
set dest=C:\te mp2\
if not exist "%dest%" mkdir "%dest%"
xcopy "%source%*" "%dest%" /t /e
for /F "tokens=*" %%a in ('dir /B /S /A-D "%source%"') do (
set fileRelPath=%%a
set fileRelPath=!fileRelPath:C:\te mp\=!
echo.>"%dest%!fileRelPath!"
)

As Magoo says, this is strange.
Anyway, while finding where this fails, you can do the same with
robocopy "%source%" "%dest%" * /e /create

Related

Copy everything under a non specific (*) folders

I'm trying to copy some non specific folders with their content, but can't find a way to do it. My problem is every time I try to use any of the copy commands it tells me it's an "Invalid number of parameters", or "0 File(s) copied", or "File not found -", or it copies everything from Folder1, (not just the non specific folders).
What I mean by "non specific" is a folder which has an unknown name, i.e. *.
This is what I have tried:
xcopy /y "C:\Folder1\Something_*" "C:\Folder2"
copy /y "C:\Folder1\Something_*" "C:\Folder2"
robocopy "C:\Folder1\Something_*" "C:\Folder2"
/+ adding " * " in front of "Something_*"
I have also tried a lot more smaller things, that I don't see a need to add.
I'm a little lost on what to do at this point, and feel like I have been looking everywhere for a solution with no luck.
Edit: i found a way to do this and it's so much better, this is how
for /R "%sourcedir%" %%A in (Something_*) do copy /y "C:\Folder1\%%~A" "C:\Folder2"
#ECHO OFF
SETLOCAL
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
SET "destdir=u:\your results"
:: Version 1 - all files arrive in the destination directory
FOR /f "delims=" %%b IN ('dir /b /ad "%sourcedir%\Something_*" ') DO (
COPY /b /y "%sourcedir%\%%b\*" "%destdir%\" >NUL 2>nul
FOR /f "delims=" %%c IN ('dir /b /s /ad "%sourcedir%\%%b" ') DO (
COPY /b /y "%%c\*" "%destdir%\" >NUL 2>nul
)
)
cls
dir/s "%destdir%"
PAUSE
:: This is a routine I use for testing. It simply deletes the directory named and restores it, empty
CALL deltreey /r "%destdir%">NUL
:: Version 2 - preserve tree structure
FOR /f "delims=" %%b IN ('dir /b /ad "%sourcedir%\Something_*" ') DO XCOPY /y /s /e "%sourcedir%\%%b" "%destdir%\" >NUL 2>nul
)
cls
dir/s "%destdir%"
PAUSE
:: This is a routine I use for testing. It simply deletes the directory named and restores it, empty
CALL deltreey /r "%destdir%">NUL
GOTO :EOF

Xcopy directory pattern matching

I need to copy source file to destination folder using bat file.
I have created this:
#echo off
set source="D:\Folder1\file.dll"
set destination="D:\Test\TestCopy*\Test\"
xcopy /s /y %source% %destination%
pause
The destination path I have is TestCopy.2.5.3.6. This numbers can change. So specifying TestCopy* is not working. Specifying TestCopy.*.*.*.* also not working.
How can I solve this?
That is not the way xcopy is used; it cannot copy directory structures/files to multiple folders, e.g. copy file random.ext to C:\folder1\test, C:\folder2\test, etc.
Also, no need to use xcopy to copy files. Just use copy instead.
To achieve this, use:
#echo off
pushd "D:\Test"
set source="D:\Folder1\file.dll"
for /F "delims= eol=" %%A IN ('dir /B /AD "D:\Test\TestCopy*"') do (
copy %source% "%%~fA\Test\"
)
popd
Or, better one-line for /F:
#echo off
pushd "D:\Test"
set source="D:\Folder1\file.dll"
for /F "delims= eol=" %%A IN ('dir /B /AD "D:\Test\TestCopy*"') do copy %source% "%%~fA\Test\"
popd
This is for the situation when you have multiple TestCopy* subfolders:
::It's better to comment out #echo off when you are testing the batch file
::#echo off
::Moved the open quote's position, so the source variable won't have quotes inside.
set "source=D:\Folder1\file.dll"
for /F "delims=" %%i IN ('dir /ad /b "D:\Test\TestCopy*"') do call :fcopy "%%i"
::Comment or remove pause when it's okay.
pause
goto :eof
:fcopy
if not exist "D:\Test\%1\Test\" goto :eof
xcopy "%source%" "D:\Test\%1\Test\"
Used a sub procedure :fcopy, pass each TestCopy* subfolder name to it.
All :: starting lines are comment lines. They are not executed.
I think it's a good habit to quote every path variable when used, but don't include the quotes themselves in the variable -- that's why I moved the quote in the set source line.

Batch file to delete images

I am trying to create a batch file that will delete images with specific names. The images will have names such as
house-200x300.jpg
car-125x250.jpg
So what I need ideally is a regular expression to target files which end in -(Num1)x(Num2).jpg
Also, the images are in various folders and sub folders so I need to do this recursively from the parent folder.
Thanks
del /S *-???x???.jpg
Perhaps you may want to change del by dir /B command at first just to check that there is not any file that have not the specified file name format, but that will be selected by this wild-card.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*.jpg" ^|findstr /i /e /r /c:"-[0-9][0-9]*x[0-9][0-9]*\.jpg"'
) DO (
ECHO DEL "%%a"
)
GOTO :EOF
This should do the job - targeting only those filenames ending with -numXnum.jpg
You'd need to set your own sourcedir
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.
Have you considered using powershell?
Regular expressions in batch files get very bad very fast as the syntax is limited.
My experience is limited, so there may very well be a better solution than this:
#echo off
FOR /F "delims=?" %%i IN ('dir /B /S ^| findstr /R "[^\.]*[0-9][0-9][0-9]x[0-9][0-9][0-9].jpg"') DO (
del /s "%%i" >nul 2>&1
)
I'm redirecting the output as I think that FOR starts to get confused about the output of del.

count folders and subfolders with batch file

I am looking to create a batch file that when given a pathway, it will count all the folders and sub folders within it. So far I am only able to gather the number of folders within the 1st level of the pathway. I will then pipe it to a text file.
Here's what I have so far:
for /f %%a in ('dir /b /ad %folder%^|find /c /v "" ') do set count=%%a
echo %count% folder(s^)>> !output!
Am I close to getting what I want? What do I need to tweek?
Thanks!
Add /s to include all subfolders:
for /f %%a in ('dir /b /s /ad %folder%^|find /c /v "" ') do set count=%%a
echo %count% folder(s^)>> !output!
A small edit of this answer: Batch file to list files and folders in a simple format
#echo off
setlocal disableDelayedExpansion
pushd %1
set "tab= "
set "indent="
call :listFolder >report.txt
exit /b
:listFolder
setlocal
set "indent=%indent%%tab%"
for /d %%F in (*) do (
echo %indent%.\%%F
pushd "%%F"
call :listFolder
popd
)
exit /b
Here's how I solved the problem.
if exist "File Count" del "File Count"
dir "%~d1%~p1\*.*" /b /s >> "File Count"
find /c "." "File Count"
First we check to see if a file exists, and if so, delete it. Then we get a list of files and dump that to our file. then run FIND on the file. FIND will handily give us a big label, so the name of the file might as well be something attractive and human readable.
I am personally interested in a specific filetype, so I changed . into *.jpg but the principle is the same.

Recursively Iterating through Directories in Batch

Here's the situation:
I have a folder with lots of sub-folders with pdf files. I want to make a batch script that goes through each sub-folder and zip the pdf files if there are over 100 of them (using 7Zip, not asking for help with that part).
This is my first time dealing with windows batch scripting and I am extremely discourage. I have spent hours on Google and I don't think I'm any wiser on the subject. I have found a lot of reference material and example code but not a whole lot of word by word breakdown on examples. I find the syntax to be extremely user unfriendly.
Anyways here's what I've got:
#echo off
for /r %%A in (.) do (
set pdfCount = "Code that gets the total number of pdf files in current directory, something like dir *.pdf?"
if pdfCount GEQ 100 (
set beginDate = "Code that gets the date of the oldest pdf, use in the zip file name"
set endDate = "Code that gets the date of the newest pdf, use in the zip file name"
"Use a 7Zip command to zip the files, I am not asking for help with this code"
DEL *.pdf
echo %pdfcount% files zipped in "Code for current directory"
)
)
pause
My understanding is that "for /r %%A in (.) do ()" is supposed to do execute the code in every sub-directory.
This script is locale dependent, meaning it depends on the way dates and times are formatted on your machine. My machine uses mm/dd/yyyy hh:mm am format. The script will create zip files with names in the form of PDF yyyy_mm_dd yyyy_mm_dd.7z.
#echo off
setlocal disableDelayedExpansion
for /r /d %%P in (*) do (
set "beg="
set /a cnt=0
pushd "%%P"
for /f "eol=: delims=" %%F in ('dir /b /a-d /od *.pdf 2^>nul') do (
set /a cnt+=1
set "end=%%~tF"
if not defined beg set "beg=%%~tF"
)
setlocal enableDelayedExpansion
if !cnt! gtr 100 (
for /f "tokens=1-6 delims=/ " %%A in ("!beg:~0,10! !end!") do (
7zip a "PDF %%C_%%A_%%B %%F_%%D_%%E.7z" *.pdf
del *.pdf
)
)
endlocal
popd
)
This may work. It's not destructive and atm just echoes the 7zip and parameters to the screen.
#echo off
for /f "delims=" %%a in ('dir /b /ad /s') do (
pushd "%%a"
for /f %%b in ('dir *.pdf /b ^|find /c /v "" ') do (
if %%b GTR 100 echo 7zip a "%%~nxa.7z" "*.pdf"
)
popd
)
It takes all the folders in the current directory tree, pushes the directory on the stack to make it current, uses dir and find to count the PDF files, and if the result is greater than 100 it will echo the line to the console. And popd pops the directory off the stack again. The 7z files will be created in the folder with the PDF files and they get the folders name.7z , unless you specify a location for them.

Resources