Batch command to open files of the same name in Notepad++ - windows

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

Related

How to replace a file at destination with another file at source location using nested for loop in cmd

Under one folder, sourcefiles for example, I have three .aar files:
D:\test\sourcefiles\netQ-1.aar
D:\test\sourcefiles\netQ-2.aar
D:\test\sourcefiles\netQ-3.aar
I want to replace those .aar files at three different locations,
D:\fido\netQ-1\netQ-1.aar
D:\fido\netQ-2\netQ-2.aar
D:\fido\netQ-3\netQ-3.aar
with the files at sourcefiles location, but only if the name of files at destination location matches their names, i.e.
replace D:\fido\netQ-1\netQ-1.aar with D:\test\sourcefiles\netQ-1.aar
D:\fido\netQ-2\netQ-2.aar with D:\test\sourcefiles\netQ-2.aar
D:\fido\netQ-3\netQ-2.aar with D:\test\sourcefiles\netQ-3.aar
For this purpose I am trying to use the nested for loop
command:
for /d %a in ('dir /b D:\test\sourcefiles\*.aar') do FOR /F "usebackq" %b in (`DIR /s /b D:\fido\\.aar`) do (if /i "%%~xa" equ "%%~xb" (replace the files ))
In this If condition I am trying to match the file names and then do a replace operation. What is the correct command to achieve this?
Given the exact input examples you gave:
from cmd
#for %i in ("D:\test\sourcefiles\*.aar") do if exist "D:\fido\%~ni\%~nxi" copy "%~i" "D:\fido\%~ni\%~nxi" /Y
and from batch file:
#echo off
for %%i in ("D:\test\sourcefiles\*.aar") do if exist "D:\fido\%%~ni\%%~nxi" copy "%%~i" "D:\fido\%%~ni\%%~nxi" /Y
This will only work if the destination folder is really the same as the file name without extension as presented in your example.
Note in both cases, remove echo only once you are happy with the results echo'd to screen. Echo is just a safety measure.

Batch file to run all .bat files in folders and sub-folders with different names

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.

Batch File to execute all .exe in a folder

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.

Windows batch command to delete files from a folder except one file

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"

dos command to delete a folder except the named sub folders

Assume I have a directory in your computer under C:/dir1
And inside "dir1" we have more directories dir11, dir12, dir13,
And in each of the above directories we have more like below
dir11- dir111, dir112 dir113
dir1-dir121, dir122, dir123
dir13 - dir132,dir132,dir133
Now I need to find a command or a small script, which can delete everything under dir1 except couple of directories say dir122 and dir132.
Can you find something using DOS commands ?
You can use dir filespec /b >tmp.bat to list the file names in a text file called tmp.bat. Then edit that file to add del before each file name you want to delete. You can remove the file names you want to keep from the batch file and then do a "change all" to add del to each line. When it is edited properly, execute the batch file.
Alternatively, you could write a quick program under vb.net to do it.
Using the given examples: this should echo all the rd commands for the other folders.
#echo off
for /f "delims=" %%a in (' dir "c:\dir1" /b /s /ad ^| findstr /v /i "dir122 dir132" ') do echo rd /s /q "%%a"

Resources