Batch file to delete specific text file from multiple locations - batch-file

I have a .bat file written that creates a text file within each sub-folder of the root folder that displays the sub-folder's contents. The issue I'm having now is creating a second .bat that will remove said text files from the sub-folder locations. The .bat I have for creating the .txt files are located below. Any help is greatly appreciated!
#echo off
IF EXIST "R:\Projects\000" PUSHD "R:\Projects\000\"
FOR /F "tokens=*" %%G in ('dir /a:d-s-h /b') do (
dir /s/b > R:\Projects\000\%%G\Folder_Contents.txt
)
POPD

It's nearly the same as your current script.
delAllFolderContent.bat
#ECHO OFF
IF EXIST "R:\Projects\000" (
PUSHD "R:\Projects\000"
FOR /F "tokens=*" %%G in ('dir /a:d-s-h /b') DO (
DEL "R:\Projects\000\%%G\Folder_Contents.txt"
)
POPD
)
I moved the FOR/F code into the IF EXIST block, so it will be executed only when the directory exists and not always.

If i truly understand you. It's look like this:
#ECHO OFF
IF EXIST "C:\Temp\" PUSHD "C:\Temp\"
ECHO #ECHO OFF > loc.bat
FOR /F "tokens=*" %%G in ('dir /a:d-s-h /b') DO (
DIR /s/b > C:\Temp\%%G\Folder_Contents.txt
ECHO DEL C:\Temp\%%G\Folder_Contents.txt >> loc.bat
)
POPD

del "R:\Projects\000\folder_contents.txt" /s

Related

Batch create a Folder in every subfolders and Move all the .jpg files in it

I made this batch file that makes a folder named Pictures and puts all the .jpg files in it.
#echo off
md Pictures
for %%i in ("*.jpg") do (
move /Y "%%i" "Pictures" )
end
Is there anyway to make the script iterate through subdirectories as well? I want to run the batch file on a root directory and make it work for all subfolders.
Based upon the answer you've supplied, I would suggest you could do that like this, in a single line batch-file:
#For /D %%G In (*) Do #"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
As an alternative, because For /D may not pick up all of your directories, (it ignores directories with the hidden and system attributes):
#For /F "EOL=? Delims=" %%G In ('Dir /B /AD') Do #"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
Actually I just figured it out
#echo off
setlocal
for /f "usebackq tokens=*" %%a in (`dir /b /a:d`) do (
rem enter the directory
pushd %%a
echo In Directory: %%a
md Pictures
for %%i in ("*.jpg") do (
move /Y "%%i" "Pictures" )
rem leave the directory
popd
)
endlocal
It does the same thing but it works through subfolders. Took me sometime to figure it out. Anyway, hopefully it'll help others too.
Try this:
for /f %%a in ('dir /b /ad "Filepath"') do (
md "%%~fa\Pictures"
for %%b in ("*.jpg") do robocopy "%%~fb" "%%~fa\Pictures\" /mov
)

Changing text in txt file with .BAT

My program is almost working but I need some help with the way the content in the .txt files are updating. My program might be messy or sloppy, I am not a good programmer and I have been copying and pasting logic from this website until things started working!!
Program logic step by step
1.Copy all .csv files from folder1 to folder2
2.Delete all existing .txt files in folder2
3.Convert all .csv files in folder2 to .txt files
4.Change data in each .txt file
Before data in txt file
.001
.002
.004
Desired After data in txt file
Input1=.001
Input2=.002
Input3=.004
Actual After data in txt file
.001
.002
.004
Input1=.001
Input2=.002
Input3=.004
I cannot have it append to the bottom of the .txt file I need it to overwrite the existing data.
Any help would be appreciated!!
#echo off
set folder1="Z:\CNC_Dev\EJV\Set Up Info"
set folder_1=Z:\CNC_Dev\EJV\Set Up Info
set folder2="Z:\CNC_Dev\EJV\PW991tetsingbat"
set folder_2=Z:\CNC_Dev\EJV\PW991tetsingbat
set fType=*.csv
set fType2=*.txt
set LineNumber=0
setlocal EnableDelayedExpansion
for /f "delims=" %%f in ('dir /a-d /b /s "%folder_1%\%fType%"') do (
xcopy /e /i /v /h /k /y "%%f" "%folder2%\" 2>nul
)
pause
del /s %folder2%\%fType2%
PAUSE
FOR /R "%folder2%" %%f IN (*.csv) DO REN "%%f" *.txt
PAUSE
#FOR /f "delims=" %%f IN ('dir /b /s "%folder_2%\*.txt"') DO (
set /a "idx+=1"
set "FileName[!idx!]=%%~nxf"
set "FilePath[!idx!]=%%~dpFf"
)
PAUSE
for /L %%i in (1,1,%idx%) do (
echo "!FileName[%%i]!"
pause
set LineNumber=0
del "%TEMP%\!FileName[%%i]!.tmp" 2>nul
for /F "usebackq delims=" %%I in ("%folder_2%\!FileName[%%i]!") do (
set /A LineNumber+=1
echo INPUT!LineNumber!=%%I
) >>"%folder2%\!FileName[%%i]!"
pause
)
echo DONE!
PAUSE

List files from certain folder using batch

I want to find the .seq files from src folder and its sub-directories. I'm trying with below approach but it doesn't create a devtest.txt file. Any help please.
For /R %%A In (src\*.seq)Do #Echo drafting.rep:%%~nxA >> devtest.txt
A FOR loop will find all of the files and run the ECHO command on them.
#SET "SEARCHDIR=C:\src"
#SET "LOGFILE=%USERPROFILE%\devtest.txt"
#IF EXIST "%LOGFILE%" (#DEL "%LOGFILE%")
#FOR /F "delims=" %%A IN ('DIR /S /B "%SEARCHDIR%\*.seq"') DO (
#Echo drafting.rep:%%~nxA >>"%LOGFILE%"
)

How to remove space from folder names in a particular directory?

I have a requirement to remove the space from the folder name in a particular directory. e.g. Project-A is my directory under which have Phase1 Testing, Phase1 Prod, Phase1 UAT subdirectories. I want a batch script which will rename the subdirectories to Phase1Testing, Phase1Prod, Phase1UA names.
you could try something like this:
#echo off
FOR /f "delims=" %%G IN ('dir /ad /b') DO (
setlocal enabledelayedexpansion
pushd "%%~dpG"
SET fname=%%~nxG
SET fname=!fname: =!
rename "%%~nxG" "!fname!"
popd
endlocal
)
You can find more information in: replace_spaces_with_dashes and in spaces_in_file_names
I hope this help you!
Thanks #Dayana for the help. Below batch script is working fine and removing the space from the current directory.
#ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /f "tokens=*" %%a IN ('DIR /s /b /ad') DO (
SET Var=%%~na
SET Var=!Var: =!
REN "%%a" "!Var!"
)

How can I modify this batch script's directory?

This batch script creates a text file of a list of files in its own directory, sans file extensions. However, I would like it to create a list of another directory's files instead. How could I modify it to attain those results?
#echo off
cd %1
if exist filelisting.txt del filelisting.txt
for /F "delims=" %%j in ('dir /A-D /B /O:GEN') do echo %%~nj >> filelisting.txt
Nevermind, I figured it out:
#echo off
cd %1
if exist filelisting.txt del filelisting.txt
for /F "delims=" %%j in ('dir "c:\Location\*" /A-D /B /O:GEN') do echo %%~nj >> filelisting.txt

Resources