I need to create a batch script that will run all .exe files in a folder. This must include subfolders.
I am running windows 7, and the batch file is stored in the root folder
I have tried several variations with no success. The two main variations are as follows
REM dir *.exe /S /B > tmpFile
REM set /p listVar= < tmpFile
REM del tmpFile
FOR %%F IN (%listVar%) DO %%F
=======================================
FOR /F "usebackq" %%F IN ('dir *.exe /S /B') DO %%F
I have looked through several variations on the above methods but none work. The top version (which I would like to avoid) only reads in one line from the file. The Bottom version outputs an unmodified "dir" command in the prompt window.
Ultimately I would like help identifying how to solve this issue without the need for a temp file.
for /r "." %%a in (*.exe) do start "" "%%~fa"
This will start all the executable files under the current folder and subfolders. Change the reference to the current folder (".") to your needs.
for %1 in (%USERPROFILE%\*.exe) do "%1"
This will start all executables in your user folder. Of course, that includes installers and stuff like that, and you probably don't want to reinstall all your apps, so replace %USERPROFILE% with the directory you want.
Related
I need a simple script to run all .bat files in folders and sub-folders with their different names. here is an example :
Main Folder> Bat Folder 1> Bat File 1.bat
Main Folder> Bat Folder 2> Bat File 2.bat
. . .
Main Folder> Bat Folder N> Bat File N.bat
There are many topics out there asking the same question but the one that really worked for me was the following :
#echo off
pushd C:\Users\%USERNAME%\Desktop\Bat Folder 1\
for /f "delims=" %%x in ('dir /b /a-d *.bat') do start "" "%%x"&timeout /t 2 >nul
popd
However the problem is the direct folder address. I can't manually enter the folder names and run them one by one. it would take a long time. I want the script to go through all folders and sub-folders ignoring their names and run them all. it would be better to have the script run inside the main folder instead of the folder address !
Not a full answer, but I think this might help give you a start:
for /r "C:\Users\%USERNAME%\Desktop" %%a in (*.bat) do start "" "%%~a"
You might need call "something.bat" or start "" cmd /c "something.bat"
I suppose, if your batch files need a working directory, you could try this:
:: Make sure you're in the correct drive
c:
for /r "C:\Users\%USERNAME%\Desktop" %%a in (*.bat) do (
cd "%%~pa"
call "%%~a"
)
pushd "\Path\To\Main Folder"
for /r %%F in ("*.bat") do start /wait "" "%%~F"
for /r will iterate recursively over all *.bat files in all subdirectories to any depth.
I found an alternative. I used the code dir /s /b *.bat > runall.bat to get the full directory list and then replaced them all with pushd C:\Users\%USERNAME%\Desktop\Bat Folder 1\Bat File 1.bat
It's definitely not a good way to go but at least it fixed my issue.
Edit: It simply doesn't work. just reads the first line and ignores the rest.
I would like to open all files called "asdf.txt" in Notepad++, found in all subfolders from the directory that the batch file is located in.
The solution found here applies to file types but does not translate to exact file names:
Batch command to open all files of a certain type in Notepad++
Here is my attempt:
FOR /R %%a IN (asdf.txt) DO "C:\Program Files (x86)\Notepad++\notepad++.exe" "%%a"
This results in Notepad++ stating that asdf.txt doesn't exist and would I like to create it. It continues to ask me for every subfolder that exists (which can be hundreds of times). If I say yes, then it creates the file asdf.txt in every subfolder. If I keep pressing yes/no, then it eventually does open the existing files. What I want, however, is to open all existing files with exactly the same name and not create new files.
your filename doesn't contain any wildcard, so it's taken as a string, not a file mask.
Either add a wildcard:
FOR /R %%a IN ("asdf.txt?") DO "C:\Program Files (x86)\Notepad++\notepad++.exe" "%%a"
or use:
FOR /F "delims=" %%a IN ('dir /s /b /a-d "asdf.txt"') DO "C:\Program Files (x86)\Notepad++\notepad++.exe" "%%a"
You could also use the Where command:
#For /F "Delims=" %%A In ('Where/R <srcDir> asdf.txt') Do #Start "" "%ProgramFiles(x86)%\Notepad++\notepad++.exe" "%%A"
Change <srcDir> to your specific source directory, (use a dot for the current directory). You can of course remove the Start "" command if it doesn't suit your specific requirements
I have a directory which looks like this:
\isa\documents\2004\2008\jac\file1.txt
\isa\documents\2004\jan\file1.txt
\isa\scannedDocs\2004\2009\jan\file2.pdf
\isa\documents\2005\2008\jac\file1.txt
\isa\documents\2003\jan\file1.txt
\isa\scannedDocs\2002\2009\jan\file2.pdf
I have a list of files I need copied (exported from a database), but because I don't need EVERY file in the directory, I only want the ones copied from my list:
Files-needed.txt:
\isa\documents\2004\2008\jac\file1.txt
\isa\documents\2004\jan\file1.txt
\isa\documents\2004\jac\file3.txt
\isa\documents\2003\jan\file1.txt
Basically:
I'll need to copy out the specific files needed, and
Log when a file can't be found, and,
I need folder structure retained, as the files might have the same name, just in different directories.
It's on a windows 7 machine, and I can run PowerShell and batch files. I tried robocopy and xcopy and either got all of the directories and no files or all files and no directories...
Any assistance would be great.
EDITS:
Okay, so i have tried Robocopy, but it is either copying the directories and no files or files and no directories. I haven't tried anything in powershell yet, but that might be the way...
It is Windows, I might have just written the slashes incorrectly above, I work between both environments, and was just trying to explain the problem.
Things I tried:
#echo off
set src_folder="C:\batchScripting\TEST_DIR\"
set dst_folder="C:\batchScripting\COPY2_DIR\"
robocopy "C:\batchScripting\TEST_DIR" "C:\batchScripting\COPY2_DIR" FileList.txt /S /V /NP /LOG:"log.log" /R:10 /W:30
and
#echo off
set src_folder=C:\batchScripting\TEST_DIR\
set dst_folder=C:\batchScripting\COPY2_DIR\
for /f "tokens=*" %%i in (File-list.txt) DO xcopy /s /i "%src_folder%\%%i" "%dst_folder%"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN (q23221996.txt) DO (
IF EXIST "%sourcedir%%%a" (ECHO f|xcopy /y "%sourcedir%%%a" "%destdir%%%a" 2>NUL >nul
) ELSE (ECHO "%sourcedir%%%a" does NOT exist)
)
GOTO :EOF
I used a file named q23221996.txt containing your data for my testing. sourcedir and destdir are both set up to suit my system.
The /y on the xcopy command forces overwrite if the destination file already exists.
I am trying to create a batch script to move a set of files from one folder (root) to another and delete files of extension .dll in the root folder except one file. The command I tried is able to copy but not delete the files.
MOVE D:\mpgdata\sync\*.txt D:\data\sync\QDB_TXT_FILES
for %%i in (d:\data\sync*.dll) do if not "%%i"=="work.dll" del /f "%%i"
It had a case sensitive compare. /i fixes that.
There was also a missing backslash.
The %%~nxi makes it compare the filename only.
MOVE "D:\mpgdata\sync\*.txt" "D:\data\sync\QDB_TXT_FILES"
dir d:\data\sync\*.dll /b
pause
for %%i in (d:\data\sync\*.dll) do if /i not "%%~nxi"=="work.dll" del "%%i"
I want to clean up my binary files in my development directory. Is there a way I can recurse through the directory structure and delete all files that are in a bin directory using the Windows command line? (I am using Windows 7.)
Per Nathan, I tried to make a batch foo.bat:
#ECHO OFF
for /R %%x in (.) do call:myExistFunc %%x
GOTO:EOF
:myExistFunc
if exist %~1\.\bin call:myDeleteFunc %%~1\bin
GOTO:EOF
:myDeleteFunc
echo. Deleting files from %~1
del %~1*.* /Q
...and this worked.
Create a batch file that contains the following:
for /R %%x in (bin) DO del "%%x\\*.*" /Q
This will recursively walk through all child directories (from the current directory) and delete all files from every BIN folder. You could change the *.* to whatever file type you'd like to delete.
This should work in a batch file:
for /R %%x in (bin) do if exist "%%x" del /q "%%x\*.*"
If running this command directly from the command line, replace all instances of %%x with just %x.
It's been awhile since I've had use any DOS but..
DEL BIN\*.*
That should delete the files in the directory named BIN below the current directory.
You should write a bat file:
for /r %%f in (*) do 'WHATEVER YOU WANT TO DO'