Windows Batch to Overwrite Existing files in folder and subfolders - file

I am not good in this. I really need help.
I have searched a lot but no luck.
I want to create empty files and replace existing in a folder and its subfolders.
From my search, I could find how to make a batch to delete.
set folder="test"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
However, I want to replace the files with empty one, I tried this:
set folder="test"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (echo. 2>"%%i")
But it is not working. I need this to replace all my existing files so after delete it should not be recovered.

Better quote the whole set command and use always quotes:
set "folder=x:\test"
cd /d "%folder%" ||(pause&Goto :Eof)
for /F "delims=" %%i in ('dir /b/A-D') do Type Nul >"%%i"
To not by mistake clear the current folder I added the ||(pause&Goto :Eof) in case the cd didn't work. This is a conditional execution on fail of the previous command

Related

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.

deleting all but one files using batch file not working

I have batch script that runs in WIn 7 command prompt.
it needs to delete all the files in folder except file with ".a" extension.
I tried following code
for /F %%I in ("*") DO if not (%%~xI=="a") del /q %%I
It deletes all files.
I tried following:
for /F %%I in ("*") DO if not (%%~xI==a) del /q %%I
no luck. Where am I wrong?
sedy
for /F loops consider sets in quotes to be strings, so you are saying to parse the string "*", which results in the delete command being interpreted as del /q *, which deletes everything.
Use for /F %%I in ('dir /b') instead, which will process based on the list of files in the current directory.
for /f "usebackq tokens=*" %%f in (`dir /b .\d\*`) do (
if not %%~xf equ .a (echo deleting %%f)
)

How to change every file content in every subfolder using a batch file?

I searched a lot but I could not found a way to overwrite every file in every sub folder using a batch file.
I've found a way to delete these files and folders:
set folder="C:\test"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
but, I don't want to just delete, I want to change its content, like this:
echo whatever > song.mp3
Yes, I know this sounds a little weird, but can I make the second process using the that loop in the first code?
for /r "c:\test" %%i in (*) do echo whatever > "%%i"

Getting Error while cleaning all files and folders from desktop and moving it to a BACKUP folder in desktop

#echo off
set a="%USERPROFILE%\Desktop\Desktop BU\%CurrentDate%"
For /F "tokens=1-5 delims=/-" %%A in ('Date /t') do (SET CurrentDate=%%A%%B%%C)
::echo hi
mkdir "%USERPROFILE%\Desktop\Desktop BU\%CurrentDate%"
::echo dir made
dir %USERPROFILE%\Desktop\ /S /B > %TEMP%\files.txt
::echo done
For /F %%A in (%TEMP%\files.txt) do move %%A "%USERPROFILE%\Desktop\Desktop BU\%CurrentDate%\"
del %TEMP%\files.txt
Having a torrid time with the above script:
- it creates the folder nicely with proper date
-creates the file in temp and redirects the output from the list in the desktop
- But after that it gives error. The path specified could not be found
i tried many things but its not working. Could you please let me know how can i move files and folders in the desktop to a folder BACKUP in the desktop. so that my desktop looks clean. And i would also like to exclude some items from being moved.
Try changing your for statement to this:
For /F "usebackq tokens=*" %%A in ("%TEMP%\files.txt") do move "%%A" "%USERPROFILE%\Desktop\Desktop BU\%CurrentDate%\"
Also, the way it is currently, your destination directory will have a space at the end, and it'll try to move Desktop BU into itself. Finally, there's no need for the /s switch in your dir command, as moving a directory will move all its children anyway. Try this instead:
#echo off
setlocal
set CurrentDate=%date:~0,2%%date:~3,2%%date:~6,4%
set desktop=%USERPROFILE%\Desktop
dir "%desktop%" /B | find /v "Desktop BU"> "%TEMP%\files.txt"
mkdir "%desktop%\Desktop BU\%CurrentDate%" 2>NUL
For /F "usebackq tokens=*" %%A in ("%TEMP%\files.txt") do move "%desktop%\%%A" "%desktop%\Desktop BU\%CurrentDate%\"
del "%TEMP%\files.txt"
Or here's another way that doesn't require an intermediate files.txt:
#echo off
setlocal
set CurrentDate=%date:~0,2%%date:~3,2%%date:~6,4%
set desktop=%USERPROFILE%\Desktop
mkdir "%desktop%\Desktop BU\%CurrentDate%" 2>NUL
for /f "tokens=*" %%A in ('dir "%desktop%" /b ^| find /v "Desktop BU"') do (
move "%desktop%\%%A" "%desktop%\Desktop BU\%CurrentDate%\"
)

How can I copy files to the "newest subdir" in a directory using a batch file?

I need to copy files from recent build folder to another folder used for testing. I'm having a hard time getting the name of the most recent build folder.
My current attempt is this:
#for /D %%i in ('dir e:\builds\projectA\* /O:D') do set target=%%i
echo %target%
xcopy "%target%\*.*" \\devbox\projectA /y /s
I was hoping target would be the newly created folder from which I could then copy the files from. However, when I echo target to the console it just says:
/O:D'
Does anyone know how I can get this to work (or know of an alternative)?
Replace the /D with /F and add /B to the bracketed dir command.
#for /F %%i in ('dir e:\builds\projectA\* /O:D /B') do set target=%%i
echo %target%
xcopy "%target%\*.*" \\devbox\projectA /y /s
pushd E:\builds\projectA
for /f "delims=" %%d in ('dir /b /a:d /o:d') do #echo %%d>latest.txt
for /f "delims=" %%l in (latest.txt) do xcopy "%%l\*.*" \\devbox\projectA /y /s
del latest.txt
popd

Resources