Batch File - FOR LOOP - windows

Let me set the stage of my issue. I have a folder (FOLDER_ABC). Within that folder, there is a folder for my application, including a unique and always changing version number (application-v1.3.4). Within that folder, there is the application (application-v1.3.4.exe) - which will also change periodically.
C:\FOLDER_ABC\application-v1.3.4\application-v1.3.4.exe
In this section below I create a directory listing of the FOLDER_ABC for any folders starting with application* and store that folder name into a file called directory.txt. I then create a perameter and store that directory into it. I'm doing it this way, versus applying the direct full path to the directory or file, since the versions will change and I don't want to hard code the batch script.
cd C:\FOLDER_ABC\
dir application* /b /ad>"C:\FOLDER_ABC\directory.txt"
set /p verdir= <C:\FOLDER_ABC\directory.txt
Here is my issue. In the section below, I'm trying to get my batch script to run the application*.exe file, and continue on with my batch file. It currently runs my application, but it hangs and doesn't continue the rest of my batch script. I'm really new to all this coding so I appreciate the help. I assume it could be something related to me not closing the FOR loop properly? How can I get it to continue on to :FINISH?
cd "C:\FOLDER_ABC\%verdir%\"
FOR /f "tokens=*" %%G IN ('dir /b *.exe') DO %%G;
:FINISH
ECHO THE END
exit
Figured it out, but didn't have enough StackOverflow credits to answer my own question. My solution is listed below. Thanks everyone. You pointed me in the right direction.
cd "C:\FOLDER_ABC\%verdir%\"
FOR %%G in (*.exe) DO START %%G

you can try:
FOR /f "delims=" %%G IN ('dir /b /a-d *.exe') DO start "" "%%~G"

I'm not sure if this is your problem, but it is possible that the ; is causing problems. You do not terminate commands with ; in batch files.
There is no need for a temporary file. Your code can be greatly simplified with FOR:
pushd c:\folder_abc
for /d %%F in (application*) do cd "%%F"
for %%F in (*.exe) do "%%F"
:FINISH
ECHO THE END
exit /b

Try using the START command:
FOR /f "tokens=*" %%G IN ('dir /b *.exe') DO START %%G;
There may be other better ways if achieving what you want, for example, if you know that the exe always has the same name as its directory, and that there will be only one such directory, you could do the following:
FOR /D %%i in (application-v*) DO START %i\%i.exe
UPDATE
From comments:
My only issue, which I just realized, is that the application folder and application name are not always identical.
In that case, you could try something like:
for /d %%i in (application-v*) do for %%j in (%%i\*.exe) do start %%j

Related

I need a batch file to generate a list with *only* file names

I need it to work on Win10 and Win7 machines. If I can get this to work I'll make a batch file.
Winkey, "cmd"
cd "e:\media\trainingvids"
dir *.* /s /b /a -d > c:\temp\dork.txt
So, to state the obvious but make sure I'm getting it, I'm opening a command prompt, changing to the correct directory, doing a directory listing of all files (including sub-directories (/s), no headers or footers so 'bare' format (/b), and trying to NOT display the directory (/a -d) – and then sending/piping that (>) to a file I've designated to be named and created (dork.txt) in a temporary directory (\temp) that already exists on my c:.
The problem is that it doesn't work. I'm not able to find a way to NOT include the full path along with the file names. I need a nudge with the syntax. Or maybe I've got it all wrong and it can't be done in this way.
What does my Basic batch file look like that can do this?
You will need the for /F command to accomplish this:
> "D:\temp\dork.txt" (
for /F "eol=| delims=" %%F in ('
dir /B /S /A:-D "E:\media\trainingvids\*.*"
') do #(
echo(%%~nxF
)
)
You placed a SPACE between /A and -D in your dir command line, which must be removed.
Since I stated the full path to the target directory in the dir command and also to the output file, you can save this script at any location and run it from anywhere.
for /f "delims=" %%a in ('dir /s/b/a-d') do echo %%~nxa
should accomplish that task.
If you can tolerate double quoted names this batch file works well.
set myPath=c:\temp
set myMask=*.pdf
set myLog=c:\temp\myLogFile.log
FORFILES /P %myPath% /S /M %myMask% /C "CMD /C ECHO #file >> %myLog%"
Alter the values to meet your needs.
You're almost there with:
dir /s /w /a-d | find "."
The only drawback is that you get the file names in columns, and possibly a Volume line which you can remove with another find filter.

Batch xcopy not working

I have a very simply batch file that I am trying to execute. The premise of it is simple: for every user, copy a folder and all its contents into another folder. However, upon executing I am unsuccessful. I do not get any error messages and it exits out immediately. I have ran the xcopy command itself with success, so this leads me to believe I am doing something wrong with the for loop. My knowledge with command prompt is relatively limited, so please pardon my ignorance in regards to the subject. Thanks for any help!
for /f "delims=" %a in ('dir /b /ad C:\Users') do xcopy C:\Folder "C:\Users\%a\AppData\Roaming\Folder" /f /j /s /w /y
It works for me from the command line (replacing xcopy with echo xcopy). From a batch file, though, you'll need to double your % signs, so make sure you're using %%a instead of %a.

Batch command to get the file path only, from a string

I've searched and found several examples on this, but I don't seem to get anything to work... I'm writing a simple Windows batch script to unzip files. In my batch script I have a variable, zipfile, that is dynamically assigned as the most recent Zip file in folder and subfolders:
for /f "tokens=*" %%a in ('dir d:\temp\*.zip /s /b /od') do set zipfile=%%a
To simplify, considering the value:
set zipfile=d:\temp\mysubfolder\myfile.zip
How can I get the full path, "d:\temp\mysubfolder\" ? Thank you!
Easy:
for /f "tokens=*" %%a in ("%zipfile%") do (set fullpath=%%~dpa)
Echo %fullpath%
Done! Make sure %zipfile% does not have surrounding quotes.
Mona
See the call /? for how to use labels inside a batch file.
It also explains how to extract the drive, path, and filename from a parameter.
set zipfile=d:\temp\mysubfolder\myfile.zip
call :SETZIPPATH %zipfile%
goto:eof
:SETZIPPATH
set zippath=%~dp1
You can also do the call from inside the for loop.

Move files in subfolders to a folder and rename windows prompt

I have almost the final command line, but I need the rename part of it, to reach the goal I want I have this:
for /f “tokens=*” %a in (‘dir /b /s /a-d’) do #copy “%a” “C:\YourFolder” /y
and it works fine, but in my case I have tons of folders with only one file on each one, this file has the same name file.ext, so, is there any way to move and change the name, for example like file1.ext, file2.ext,...
Thanks!
This is not foolproof but it's likely to work, and will prompt if the small likelihood occurs that a filename exists. You'll need a batch file to make it foolproof and to improve the naming strategy.
Remove the #echo if you are happy with the result.
cmd /v:on /c for /f "delims=" %a in ('dir /b /s /a-d') do #echo copy “%a” “C:\YourFolder\%~na-!random!!random!!random!%~xa” /-y

Batch Rename Subdirectories in Windows

I have a directory with thousands of subdirectories that contain their own subdirectories that need to be renamed. I'm using a Windows 7 machine that I do not have Administrator rights for so I can't download a simple program to do this for me.
Right now I have a test directory C:\test with a few subdirectories that have subdirectories named old that I am trying to change to new using a batch file.
Just to be clear I want the following:
C:\test\1\old
C:\test\2\old
C:\test\3\old
to become
C:\test\1\new
C:\test\2\new
C:\test\3\new
Thank you for any help you can provide.
Here's what I came up with quickly. I ran a quick test locally and it seemed to do what you're asking for:
#echo off
FOR /D %%D IN ("C:\test\*") DO CALL :RENAME %%D
:RENAME
SET CRITERIA=\old
FOR /D %%R IN (%1%CRITERIA%) DO RENAME %%R "new"
Save that to a bat file and give it a shot. Hopefully that helps.
Justin's answer was really helpful for my similar problem, though by default it only handled a simpler pattern for \A\B\C where A is a base directory, B is some undetermined directory and C is the directory you seek.
I modifed his script to recurse the base directory A through any number of layers until it finds C.
Here's the script, written to expect command line paramers:
#echo off
set BASEDIR=%1
SET CRITERIA=\%2
SET REPLACENAME=%3
call :FindDirs %BASEDIR%
GOTO END
:FindDirs
FOR /D %%F IN ("%1\*") DO CALL :RENAME %%F
GOTO:EOF
:RENAME
REM echo DIR=%1
FOR /D %%R IN ("%1%CRITERIA%") DO (
if EXIST %%R RENAME %%R "%REPLACENAME%"
)
call :FindDirs %1
GOTO:EOF

Resources