Using some components I found here, I have built a batch file to loop through a directory tree starting from the directory where the batch file runs.
The batch file works as expected but I need to capture the output from the cmd.exe command CD into a file that I created earlier in the run.
The problem is that if I attempt to redirect the standard output into the .txt file I only see the first found directory.
I have found some code which uses PowerShell, to pull the listing from the Command Prompt screen, but to me this is inelegant, (although it seems to work).
I have read the material on setlocal enabledelayedexpansion but it appears to be above my paygrade as I haven't been able to make it work.
The working code is below, with a Remark where I think the export to the .txt file should go.
Help would be appreciated.
Rem Recursively Traverse a Directory Tree
Rem Notes:
Rem "For /r" command can be used to recursively visit all the directories in
Rem a directory tree and perform a command in each subdirectory.
Rem In this case, save the output to a text file
Rem for /r = Loop through files (Recurse subfolders).
Rem pushd = Change the current directory/folder and store the previous folder/path for
Rem use by the POPD command.
Rem popd = Change directory back to the path/folder most recently stored by the PUSHD
Rem command.
#echo off
CLS
echo.
echo.
Rem FirstJob - Generate a date and save in the work file.
Rem Grab the date/time elements and stuff them into a couple of variables
set D=%date%
set T=%time%
set DATETIME=%D% at %T%
Rem OK. We now have the date and time stuffed into the variable DATETIME
Rem so now stick it into our work file along with a heading.
Echo List of Found Directories > DirList.txt
Echo %DATETIME% >> DirList.txt
echo. >> DirList.txt
echo. >> Dirlist.txt
Rem SecondJob - Do the looping stuff and save found directories to file.
Rem Start at the top of the tree to visit and loop though each directory
for /r %%a in (.) do (
Rem enter the directory
pushd %%a
CD
Rem ------------------ direct Standard Output to the file DirList.txt -----------------
Rem exit the directory
popd
)
: END
Rem All finished
Echo Done!
exit /b
The additional lines of code when added to the above scrip before the :END marker. which did produce the wanted output were:
powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('^a')
powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('^c')
powershell Get-Clipboard>>DirList.txt
The problem is your pushd command, because you change the current directory, the file dirlist.txt must use an absolute path, else you create it in every pushed directory.
I used %~dp0 here, it's the path of the batch file itself.
You could try
cd >> %~dp0\dirlist.txt
Or just
echo %%a >> %~dp0\dirlist.txt
Or you could use a single redirecton of the complete bock
(
for /r %%a in (.) do (
pushd %%a
echo %%a
popd
)
) > dirlist.txt
I am new to batch. I work with website, I have a folder called web, in this folder, I have many sub directories, such as 'template1', 'template2', etc. In those templates, there is one thing in common, each only contains three same files, 'draw.html', 'draw.js', 'draw.css'. Each time I want to create a new page I need to manually create this pattern. I wonder if I can code a batch file, so I can do this by click on pat.bat, and it generate the folder and three files for me. after thinking, I decide to do this: in web folder, create two files: ['pat.bat', 'pat.txt'] in 'pat.txt', there are three strings seperated by line: [draw.html, draw.js, draw.css], the logic in batch file is pretty simple: it asks the user to input a directory name, then generate
directory_name
|-draw.js
|-draw.html
|-draw.css
here's the batch file I worked out <pat.bat>
#echo off
:dir_loop
echo Create a directory named:
set /p directory=
if "%directory%" equ "" (
echo Please enter a valid directory
goto :dir_loop
)
:pat_loop
if not exist pat.txt (
echo Please create a pat.txt file which
echo contains all your files separated by
echo line with correct extension to
echo generate into the folder.
echo "Have you done it? (y/n)"
set /p good=
if not exist pat.txt (
echo Sorry, no pat.txt detected in current directory
goto :pat_loop
)
)
:final
mkdir %directory%
for /F "delims=," %i in (pat.txt) do (
cd %directory%
cd. > %i
cd..
)
pat.txt
draw.js
draw.css
draw.html
ok, here comes the problem, when I change the 'directory' variable to a default value rather than user input, and run this in the elevated command prompt inside my web directory, it works fine (following code)
#echo off
:dir_loop
echo Create a directory named:
rem !!! change directory from user input to assignment
set directory=template1
if "%directory%" equ "" (
echo Please enter a valid directory
goto :dir_loop
)
:pat_loop
if not exist pat.txt (
echo Please create a pat.txt file which
echo contains all your files separated by
echo line with correct extension to
echo generate into the folder.
echo "Have you done it? (y/n)"
set /p good=
if not exist pat.txt (
echo Sorry, no pat.txt detected in current directory
goto :pat_loop
)
)
:final
mkdir %directory%
for /F "delims=," %i in (pat.txt) do (
cd %directory%
cd. > %i
cd..
)
but, if I set the variable 'directory' to user input rather than string, when I click on the <pat.bat> file, entry my folder name for example, ak, it creates an ak folder in web with nothing in it. (first <pat.bat> code)
I guess it's my for loop's variable assignment issue, I will be so glad if you can help.
PS: you can test the code by simply create a folder on desktop, create two files inside of it pat.txt, pat.bat and paste it in. to test the second bat file, type cmd in the folder, paste in the code (make sure pat.txt is created at first)
#echo off
cd %~dp0
:dir_loop
echo Create a directory named:
set /p directory=
if "%directory%" equ "" (
echo Please enter a valid directory
goto :dir_loop
)
:pat_loop
if not exist pat.txt (
echo Please create a pat.txt file which
echo contains all your files separated by
echo line with correct extension to
echo generate into the folder.
echo "Have you done it? (y/n)"
set /p good=
if not exist pat.txt (
echo Sorry, no pat.txt detected in current directory
goto :pat_loop
)
)
:final
mkdir %directory%
for /F "delims=," %%i in (pat.txt) do (
cd %directory%
cd. > %%i
cd..
)
This code worked for me in a folder in the path C:\Users\Neko\Desktop\Test. There were several issues with your code.
As #Mofi said, in batch files, the command extension of for loop variables get expanded to %%var instead of %var so you have to change your %is to %%i.
This code would automatically execute in the %HOMEPATH% directory (C:\Users\<User>) which would cause errors in finding pat.txt, I changed it so that it executes in the parent directory of pat.bat, %~dp0 (parent directory of batch file). This would look for pat.txt in the same folder as the batch file (which for me was C:\Users\Neko\Desktop\Test) and then makes the folder in that directory. (you can change it by editing the :final label to include a cd <path you want folders in>
Again, like #Mofi said, %%i instead of %i is needed in batch-files. Your error was caused by this since the for loop wouldn't execute.
I ran this file with the circumstances you suggested (you can test the code by simply create a folder on desktop, create two files inside of it pat.txt, pat.bat and paste it in. to test the second bat file, type cmd in the folder, paste in the code (make sure pat.txt is created at first)) and it worked for me, it created draw.js, draw.css, and draw.html in the C:\Users\Neko\Desktop\Test\<%directory%> folder succesfully. (When %directory% is abc it creates draw.js, draw.css, and draw.html in the C:\Users\Neko\Desktop\Test\abc folder)
I have legal .strm files of different TV Shows in a folder named TV Shows. Each strm file of each episode is stored in different subfolders.
I would like to run a certain VBScript before these strm files are played.
Then I have a different folder named MOVIES. Again, I would like to run a VBScript before these strm files are played. But this VBScript is a different one.
How would I do that?
Platform is Windows.
Thanks!
If you're looking to open or do something to each file based on extension, An easy way to do this is to use an for loop with /R to search all sub-directories for an *.strm file.
#ECHO OFF
SET "LOC=C:\Folder"
CD %LOC%
FOR /R %%A IN (*.strm) DO (
Rem | Do something with each file.
Start cmd.exe /C "notepad.exe "%%A""
)
Echo No More Items Found!
pause
goto :EOF
%%A will return the file path.
Start cmd.exe /C "" will start a CMD command. (Replace with your code)
notepad.exe "" will open the file (Used as an example)
EDIT:
So you're looking to check if the file exists then if true run the script? Bellow should solve that.
#ECHO OFF
If exist (C:/Path/file.strm) (
Rem | File exists, run script
Echo Do something
)
GOTO :EOF
I'm publishing content to autogenerated folders and after the publishing has finished I want to copy files to that folder based on the foldersname using a batch
The autogenerated folders always have a language name, for example German, Dutch, French and English.
What I want my batch to do is that when the folder name is German it copies all the files from C:\Sourcefolder\DE\ to the new generated folder I'm running the batch from. I've tried to find something myself but my lack of knowledge results in this:
CHDIR /D %1
#ECHO OFF
SETLOCAL
SET "sourcedir=%cd%"
IF "%1"=="German" goto :German
:German
xcopy /Y "C:\Sourcefolder\DE\*.jpg" "%1"
GOTO :EOF
Is there anyone who can help me in the right direction?
Thanks in advance!
Here is the code for a batch file which might do what you want.
#echo off
rem Is this batch file called without any parameter?
if "%~1"=="" (
echo.
echo Run %~nx0 with language as first parameter.
echo.
echo Example: %~nx0 English
echo.
pause
goto :EOF
)
if /I "%~1"=="German" set "ShortName=DE" & goto CopyFiles
if /I "%~1"=="English" set "ShortName=EN" & goto CopyFiles
rem This batch file was called with a (language) string not listed above.
echo.
echo Error: "%~1" is not a supported language.
echo.
pause
goto :EOF
:CopyFiles
rem Copy all JPEG files of the specified language from source folder
rem to the specified language folder in current working directory.
xcopy /H /I /K /Q /R /Y "C:\SourceFolder\%ShortName%\*.jpg" "%~1"
set "ShortName="
Some notes additionally to the comments in the batch code:
%~1 is replaced by cmd.exe on execution of the batch file with the string of first parameter with removing double quotes if batch file was called for example with "English" instead of just English as first parameter.
If you want to know more about %~1 or %~nx0 (name of batch file with extension but without drive and path), open a command prompt window, run there call /? and read help output for this command.
/I option of command if makes the string comparison case-insensitive.
The ampersand in set "ShortName=DE" & goto CopyFiles concatenates the two commands set and goto on a single line which makes it possible here to specify 2 commands for each if without using parentheses. See Conditional Execution for details about this special operator.
For details on the switches used on command xcopy run in a command prompt window xcopy /? and read output help.
By the way: The batch files copies the JPEG files into specified language subfolder of current working directory. The current working directory can be the directory the batch file is stored, but can be also a different directory depending on how batch file was started and from which directory. If you want to make sure that the JPEG files are copied into specified language subfolder of batch file directory, you would need for the line with xcopy:
xcopy /H /I /K /Q /R /Y "C:\SourceFolder\%ShortName%\*.jpg" "%~dp0%~1"
I have a batch file that runs when a self extracting file is executed.
The self extracting files must be copied to a specific directory on the hard disk.
In the batch file the user is asked where the path is (if it's not located in the default place).
Part of the batch file:
#ECHO OFF
IF EXIST "C:\Program Files\program\program.exe". (
set PROGRAMPATH=C:\Program Files\
) ELSE (
echo Program folder was not found. Please enter the path for Program
set /p PROGRAMPATH=Path:
)
echo Copying data to "%PROGRAMPATH%"...
copy /Y "*.txt" "%PROGRAMPATH%"
Now for my question.
If a user then enters a new path, is it possible to save that path. So when he executes the self extracting file again, it could remember that new path?
You can save the path to some file under %USERPROFILE% by doing
echo %PROGRAMPATH% > "%USERPROFILE%\AppData\Local\progpath.txt"
and then read it by doing
set /p PROGRAMPATH=<"%USERPROFILE%\AppData\Local\progpath.txt"
The full batch will look like this
#ECHO OFF
set PROGRAMPATH=C:\Program Files\
IF EXIST "%USERPROFILE%\AppData\Local\progpath.txt". (
set /p PROGRAMPATH=<%USERPROFILE%\AppData\Local\progpath.txt
)
IF NOT EXIST "%PROGRAMPATH%\program.exe". (
echo Program folder was not found. Please enter the path for Program
set /p PROGRAMPATH=Path:
)
echo %PROGRAMPATH%>"%USERPROFILE%\AppData\Local\progpath.txt"
echo Copying data to "%PROGRAMPATH%"...
copy /Y "*.txt" "%PROGRAMPATH%"