Looping folder names to a variable using Batch? - variables

I'm wanting to change the icon of every sub folder in a particular directory, can I do this by means of a loop similar to the %%~nxf used for files (I’m after folders not files)
attrib -h -r C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo [.ShellClassInfo] >C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo IconFile= C:\ICT\AutoCAD_2010\Customisations\AO2\ICO\FolderIcon.ico >>C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo InfoTip=EXE file location... >>C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
echo IconIndex=0 >>C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
attrib +h +r C:\ICT\AutoCAD_2010\Customisations\AO2\EXE\desktop.ini
attrib +r C:\ICT\AutoCAD_2010\Customisations\AO2\EXE
Of cause do this a couple of hundred times in a batch and well you get the idea.

You may process folders (not files) with /D switch of for command:
for /D %%a in (*) do echo Folder: %%a
You may also combine /R with /D to process every subfolder beneath the given path. For further details, type: for /?

Related

"Echo is OFF" text always inserted into batch file when SET variable is written to a batch file IF the file is made by another batch program

I am trying to create a batch file using another batch program using:
#echo {code here}>>batch-program.bat, but whenever I try to write code to write the contents of a SET variable to a text file, the batch program does not write the code into the other batch file, but instead writes "Echo is OFF."
Code is here:
#echo off
#echo #echo off>>apt.bat
#echo color 2A>>apt.bat
#echo echo example-batch>>apt.bat
#echo cd C:/Users/Default/apt/assets>>apt.bat
#echo mkdir cmdInput>>apt.bat
#echo cd C:/Users/Default/apt/assets/cmdInput>>apt.bat
#echo set /p cmdInput= cmd->>apt.bat
#echo %cmdInput%>>used-cmdInput.txt>>apt.bat
#echo pause>>apt.bat
This should have created a batch file named apt.bat, and written into the batch file:
#echo off
echo color 2A
echo example-batch
cd C:/Users/Default/apt/assets
mkdir cmdInput
cd C:/Users/Default/apt/assets/cmdInput
set /p cmdInput= cmd-
%cmdInput%>>used-cmdInput.txt
pause
but the 9th line (%cmdInput%>>used-cmdInput.txt)
is instead converted into the text line "Echo is OFF".
Have I done anything wrong, or is it just a really weird bug?
EDIT: I found another problem in the program, that because mkdir cmdInput is always run when apt.bat is run, so it displays a error message because of apt.bat trying to create the directory cmdInput though it already exists. apt/assets. So I have changed the code a bit, so that the directory cmdInput is created in the first "creation" batch file (the program that was used to create apt.bat). mkdir cmdInput has been removed from apt.bat.
You need to escape > with ^ but you need to escape % with %
#echo %%cmdInput%%^>^>used-cmdInput.txt>>apt.bat
unless you want to output the contents of cmdinput where you need
#echo %cmdInput%^>^>used-cmdInput.txt>>apt.bat
You can add 2>nul to the end of a md command to suppress the error message generated if the directory already exists.
You should use backslashes \ in directorynames, not forward slashes /. In winbat, the forward slash is often used for command switches. Sometimes forward slashes will work for directorynames, but backslashes always work.
You just have to "escape" the percent-signs with another % and other special chars like > with a caret) to prevent evaluation of your variable %cmdInput% (which probably is empty - therefore the Echo is off).
Also a single #echo off is sufficient. No need to add a # to every line.
#echo off
echo #echo off>>apt.bat
echo color 2A>>apt.bat
echo echo example-batch>>apt.bat
echo cd C:/Users/Default/apt/assets>>apt.bat
echo mkdir cmdInput>>apt.bat
echo cd C:/Users/Default/apt/assets/cmdInput>>apt.bat
echo set /p cmdInput= cmd->>apt.bat
echo %%cmdInput%%^>^>used-cmdInput.txt>>apt.bat
echo pause>>apt.bat
A more elegant way is to use only a single redirection (cmdhas to open the file for writing just once):
#echo off
(
echo #echo off
echo color 2A
echo echo example-batch
echo cd C:/Users/Default/apt/assets
echo mkdir cmdInput
echo cd C:/Users/Default/apt/assets/cmdInput
echo set /p cmdInput= cmd-
echo %%cmdInput%%^>^>used-cmdInput.txt
echo pause
)>apt.bat

how to save a tree as the active username using a pipe

I am currently trying to make a file that will run a tree command that saves the file as the name of the active user. %userprofile% has C:\ in it and wont work. how do i set %user% to be the name of the user? if this is impossible, then is there a way i can save it as the output of a hostname command?
#echo off
cd %userprofile%
tree /a /f > "J:\folder1\%user%"
Attrib +H +S "J:\folder1\%user%"
Replace %user with %USERNAME%:
#echo off
cd %userprofile%
tree /a /f > "J:\folder1\%USERNAME%"
Attrib +H +S "J:\folder1\%USERNAME%"
Also, I suggest you look into the set command. If you run it without any parameters it will show you all the available environment variables.

Batch file which saves user input path with spaces to a text file. (win 7)

First of all i am a noob in programming.
I am trying to make a batch file which takes an installed directory of a program as user input when run for the first time (means it should not ask for the directory the second time it is run). By searching for various scripts, i reached till here,
#echo off
Echo =============================================
echo Directory
Echo =============================================
setlocal enableextensions enabledelayedexpansion
set /p mypath=Please specify install directory;
Echo %mypath% ----was what you typed
pause
echo start>temp.txt
echo %mypath%>>temp.txt
echo \programfolder\program.exe>>temp.txt
echo -argument -argument>>temp.txt
setlocal enabledelayedexpansion
set FINAL=
for /f %%a in (temp.txt) do (
set FINAL=!FINAL!%%a
)
echo %FINAL%>input.txt
del /q temp.txt
Pause
start "<input.txt"
This saves the input path in the "input.txt" text file, and runs the program the next time it is launched.
I want the text file to have the saved path as "start driveletter:\foldername\foldername with spaces\programfolder\program.exe" -arguments
However the "start", "program folder", "program.exe" and "-arguments" are fixed.
The user input path should get saved in- %mypath%.
The does what you asked, I think:
#echo off
if exist "go.bat" go.bat
set /p "mypath=Please specify install directory; "
Echo "%mypath%" ----was what you typed
pause
>"go.bat" echo #start "" "%mypath%\programfolder\program.exe" -argument -argument

Batch for loop through variables, can't cd to each?

I have a for loop in a batch file:
#echo off
set logpath1=C:\path\to\first\log
set logpath2=C:\path\to\second\log
FOR %%G IN (%logpath1% %logpath2%) DO (
pushd %%G
pushd ..\
for %%D IN (%CD%) DO SET "dirname=%%~nxD"
popd
echo Will prefix files in %%G with %supername%
:: do operations.
popd
)
However, this doesn't seem to change my working directory. If I run the script from
C:\tmp\path\to
then I get
Will prefix files in C:\path\to\first\log with tmp
Will prefix files in C:\path\to\second\log with tmp
My purpose is to zip up log files in multiple directories. I've tried chdir, cd /d, and &~pd0 instead of %CD%, and searched high and low. Can anyone tell my why this doesn't work??
Put
setlocal enabledelayedexpansion
at the top of your batch file and use !CD! instead of %CD% in the loop. Depending on where %supername% is set you may need to use ! there too.

Batch script file to convert every file in every folder one at a time

What im trying to do is convert .ogg files stored in many different folders. What i NEED the batch script to do is go from the first folder and file , convert it and only then move on to the next file in the folder. Also during the conversion process a wave file is created, so i need it to be deleted at the end of the files conversion along with the original .ogg file. This is my code so far. It goes through every folder and file performing the oggdec producing massive amounts of wave files. I need the script to only do one folder and one file at a time
for /r %%i in (*.ogg) do oggdec "%%i"
for /r %%i in (*.ogg) do del "%%i"
for /r %%i in (*.wav) do lame -m m "%%i"
for /r %%i in (*.wav) do del "%%i"
#echo off
etlocal enabledelayedexpansion
set deletestring=.wav
echo Ready to start
echo.
echo.
for /f "delims==" %%F in ('dir /b /s /a-d ^| find "%deletestring%"') do (
set oldfilename=%%~nxF
set pathname=%%~dpF
set newfilename=!oldfilename:%deletestring%=!
Ren "!pathname!!oldfilename!" "!newfilename!"
)
echo.
echo All done
(
This code above almost works in that the end result is working, but unfortunately i need it to convert only one file at a time and delete the original .ogg and .wav before moving to the next .ogg file.So i need it to follow this rule
Take the first folder and the first .ogg file do oggdec on file producing the .wav del the .ogg file do lame -m m on the .wav file producing the .mp3 file del the .wav file move on to the next .ogg file until all files in the folder are converted then move on to the next folder which may not be in the same folder.
An example folder structure.
Sounds/voice/greek
Sounds/voice/English
Sounds/voice/russion
Using enhanced substitution for FOR variable:
%%~di - drive letter
%%~pi - file path
%%~ni - file name without extension
Resulting script is:
for /r %%i in (*.ogg) do (
oggdec "%%i"
del "%%i"
lame -m m "%%~dpni.wav"
del "%%~dpni.wav"
)