Searching Doc and Docx File in subfolders - batch-file

I want to search and store the doc and docx file path to the variables. Below mentioned "BaseDir=%p%" in p variable contains the path "X:\NEW-ENDORSEMENTS\Backlog_checked\cne\CLIENT-SUPPLIED\Backlog Transfer\Non EV articles\cne24314\Input_Files". In this the path contain a doc and docx files in sub-folders.
When i run the below code, the error message is shown "FINDSTR is not recognizable". Kindly check and clear.
set "BaseDir=%p%"
For /f "delims=" %%A in ('
Dir /B/S/A-D "%BaseDir%\*.doc" ^| Findstr /I "\\doc\\[^\\]*\.doc$"
') Do set doc_path="%%A"
for /f "tokens=*" %%b in ("%doc_path%") do (set doc="%%~dpb")
set BaseDir=%p%
For /f "delims=" %%A in ('
Dir /B/S/A-D "%BaseDir%\*.docx" ^| Findstr /I "\\doc\\[^\\]*\.docx$"
') Do set docx_path="%%A"
for /f "tokens=*" %%b in ("%docx_path%") do (set docx="%%~dpb")
echo %docx%
echo %doc%

Just in case there may be more than one subdirectory containing .doc and .docx files, the following example, may propagate the variables, %$.doc% and %$.docx%, with all of them:
#Echo Off
SetLocal EnableDelayedExpansion
Set "BaseDir=X:\NEW-ENDORSEMENTS\Backlog_checked\cne\CLIENT-SUPPLIED\Backlog Transfer\Non EV articles\cne24314\Input_Files"
For /F "Delims=" %%A In ('Where /R "%BaseDir%" *.doc *.docx 2^>Nul'
) Do If "!$%%~xA!"=="" (Set "$%%~xA=%%~dpA/") Else (
If "!$%%~xA:%%~dpA/=!"=="!$%%~xA!" Set "$%%~xA=!$%%~xA! "%%~dpA/"
)
For %%A In ($.doc $.docx) Do If Not "!%%A!"=="" Set "%%A="!%%A:\/="!"
Set $.doc 2>Nul
Pause
The last two lines are included just to show you the variables with their content, and can be substituted with your own commands as necessary.This script has limitations, the main ones being it will not function in Operating Systems pre-Vista, it does not expect directory paths which contain ! characters and due to the length of your base directory path, the size of the variable environment may be exceeded.

Why so many for loops? Why the need of findstr if you want the path to any doc or docx file?
For /f "delims=" %%A in ('Dir /B/S/A-D "%BaseDir%\*.doc*"') Do (
If "%%~xA"==".doc" echo %%~xA path "%%~dpA"
If "%%~xA"==".docx" echo %%~xA path "%%~dpA"
)
If you still want to set them as variables, you need delayedexpansion for setting variables in the parenthesized loop.

Related

Batch file, removing leading space from folder doesn't work in Win10 but does in WinXP

I am new to writing batch programs. I have thousands of folders that have a leading space character in their name and I need to remove the space. By perusing the Stack Overflow site, I have put together some code that performs as expected in WinXP, but NOT on my Win10 system.
#echo off
for /d %%A in (" *") do #for /f "tokens=*" %%B in ("%%A") do #ren "%%A" "%%B"
The code snippet above works just fine in WinXP. For example, a folder may be named " L700" but the name gets correctly changed to "L700" after running the code. However, on my Win10 system, the above code doesn't change anything with the filename.
Full code is below:
#echo off
rem Prepare environment
setlocal enableextensions disabledelayedexpansion
rem configure where to start
set "root=C:\Test"
rem For each file under root that match indicated pattern
for /r "%root%" %%f in (*,*,*.xlsm) do (
rem Split the file name in tokens using the comma as delimiter
for /f "tokens=2 delims=," %%p in ("%%~nf") do (
rem Test if the file is in the correct place
for %%d in ("%%~dpf.") do if /i not "%%~p"=="%%~nd" (
rem if it is not, move it where it should be
if not exist "%%~dpf\%%~p" md "%%~dpf\%%~p"
move "%%~ff" "%%~dpf\%%~p"
)
)
)
rem line below removes space from beginning of folder name
for /d %%A in (" *") do #for /f "tokens=*" %%B in ("%%A") do #ren "%%A" "%%B"
#ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
FOR /d %%a IN ("%sourcedir%\ *") DO (
ECHO "%%a"
for /f "tokens=*" %%b in ("%%~nxa") do ECHO ren "%%a" "%%~nxb"
)
GOTO :EOF
ren command has been disarmed and is simply echoed for safety until script action is verified.
I've no access to an XP system, but I'm surprised it worked on XP. The issue is that %%a contains the full path so you need to select only the name and extension for the rename and the leading-space-suppression mechanism.

How to search in folder excluding subfolders?

Currently I am using this batch script and it loops through all subfolders, but I don't want that. I want it to only loop through the main folder and not subfolder.
%1 is the folder path for file search
set SIGNTOOL="%~dp0Signtool.exe"
set PFXFILE="%~dp0Temporary_Signing.pfx"
set PASSWORD=12345
SET FILES="%~dp0Signing_Files_List.txt"
set TIMESTAMP="http://timestamp.verisign.com/scripts/timestamp.dll"
set "FILESPATH=%1"
FOR /F "delims= " %%a IN ('type %FILES%') DO (
FOR %FILESPATH% %%f IN (%%a) DO (
%SIGNTOOL% sign /f %PFXFILE% /P %PASSWORD% /t %TIMESTAMP% %%f
)
)
Gives error at FOR %FILESPATH% %%f IN (%%a) DO
%FILESPATH% was unexpected at this time.
Since you've omitted to include data samples, it's not clear what you are trying to do.
FOR %FILESPATH% %%f IN (%%a) DO (
You appear to have modified a for /r here since you are referring to subdirectories.
Perhaps
FOR %%f IN (%FILESPATH%\%%a) DO (
will scan yor files. you will probably need to use %FILESPATH%\%%f instead of %%f in your sign line.

Playing a The First Video located in a random folder/subfolder

I would like to make a .bat file that will open the first file within a random folder/subfolders, in the same location as the .bat file.
The code I currently have only opens a random file.
#echo off
setlocal enableextensions disabledelayedexpansion
set "rootFolder=G:\Movies\Anime"
for /f "usebackq tokens=1,* delims=:" %%a in (`
cmd /q /v /e /c "for /f delims^= %%a in ('dir /a-d /s /b "%rootFolder%"') do echo(!random!:%%a"
^| sort 2^>nul
^| cmd /q /e /v /c "set /p ".^=" & echo(!.!"
`) do start "" "%%~b"
I also have a .bat file that generates a text file with a list of all folders in the same location. I'm not sure if it would be easier to reference that.
dir /b > Animelist.txt
Also if possible how to exclude it opening particular types of files such as jpegs / the other .bat file?
You can try with this
#echo off
setlocal enableextensions disabledelayedexpansion
set "rootFolder=%cd%"
set fileTypes= "*.avi" "*.mpeg" "*.mkv"
pushd "%rootFolder%" && (
for /f "usebackq tokens=1,* delims=:" %%a in (`
cmd /q /v /e /c "set p=&for /r %%a in (%fileTypes%) do if not !f!==%%~dpa (set f=%%~dpa&set /a ((%random% %% 16273^)+1^)*!random!&echo :%%~dpa)"
^| sort 2^>nul
^| cmd /q /e /v /c "set /p ".^=" && echo(!.!"
`) do pushd "%%~b." && (
for /f "delims=" %%c in ('
dir /b /a-d /on %fileTypes% 2^>nul
^| cmd /q /e /v /c "set /p ".^=" && echo(!.!"
') do start "" "%%~fc"
popd
)
popd
)
Decomposing the task in pieces
Configure where to search and what to search (first two set)
Change to the starting folder (pushd)
Execute a recursive search for the indicated file types and output the name of the folder where the file has been found (first cmd inside for /f
As more than one file can be found in the same folder, check that we will not output a duplicate element (if inside first cmd)
For each folder, generate a random number as a prefix (set /a) and output the folder (echo)
Sort the list on the random number (sort)
Get the first folder in the list. As the list is sorted on a random number, this folder has been random selected (second cmd inside for /f %%a)
Discard the random number and retrieve only the folder (the reason for the delims and tokens in the for /f %%a)
Change to the selected folder (pushd)
List the files of the indicated types inside the selected folder (dir)
From this list select only the first file (cmd)
Retrieve the selected file (for /f %%c)
Start the selected file (start)
Return to previous folder (popd)
Return to the starting folder (popd)
The random selection of a folder is unambiguous.
The first video file is a bit more difficult. If using dir with several extensions like #aschipfl suggested, this predetermines which extensions are looked for and found first.
The other way with excluding file types is more tedious without knowing which types might occur.
Here my batch Edited Streamlined some parts:
#echo off
setlocal enableextensions Enabledelayedexpansion
Set Cnt=0
Set "Exclude=.bat$ .cmd$ .jpg$ .jpeg$ .txt$"
Pushd "G:\Movies\Anime"
(Echo::: Numbered List of folders
For /f "delims=" %%F in (
'Dir /B/S/AD/ON'
) Do Set /A Cnt+=1&Echo:!Cnt!:%%F
)>DirList.txt
:: Get Random num 1..Cnt
Set /A RndDir=%Random% %% Cnt+1
:: Get random folder name
For /f "tokens=1,* Delims=:" %%F in (
'Findstr "^%RndDir%:" DirList.txt '
) Do Set "DirName=%%G"
Echo selected %RndDir% of %Cnt% = folder %DirName%
Pushd "%DirName%
Set "FileName"
For /f "Delims=" %%F in (
'Dir /B/A-D ^|findstr /i /V "%Exclude%"'
) Do If Not defined FileName Set "FileName=%%~F"
If defined FileName Start "" "%FileName%"
Popd
Popd
Goto :Eof

batch script how to create a string out of different variable parts

in my company we create software for different customers to handle our machines. As each product is unique, so is the control software, but not completely new. So for a start we copy an old project, rename it and change it until it fits.
Usually the directory name is the name for the new program (our ide uses the directory name, but also relies on some other files following the same name scheme).
For the renaming I've wrote a short batch script which finds the old name scheme and retrieves from the directory name the new one.
But the only solution I've found for this uses a new batchfile for each file to be renamed.
Is there a better way to get the content of !progNeu! ?
#echo off
SETLOCAL enabledelayedexpansion
set pfad=%CD%
for /d %%A in (%pfad%) do (set progNeu=%%~nxA)
for /f "tokens=1,2 delims=|" %%B in ('dir /b *.s19 ^| findstr /v "appl"') > do (
set progAlt=%%B
set rumpfAlt=!progAlt:.s19=!
>x ECHO !rumpfAlt!&FOR %%C IN (x) DO SET /A strlength=%%~zC - 2&del x
for %%D in (!rumpfAlt!*.*) do (
set progAlt=%%D
>x.bat echo #echo off
>>x.bat echo set ausg=!progAlt!
>>x.bat echo echo %%ausg:~!strlength!%%
for /f "" %%E in ('x.bat') do (
set "dateiNeu=!progNeu!%%E"
if exist !dateiNeu! del !dateiNeu!
rename %%D !dateiNeu!
)
del x.bat
)
)
If I have not missed something, this could be the equivalent to your code
#echo off
setlocal enableextensions disabledelayedexpansion
set "pfad=%CD%"
for /d %%A in ("%pfad%") do (
for /f "delims=" %%B in ('
dir /b *.s19 ^| findstr /v "appl"
') do for %%D in ("%%~nB*.*") do (
set "progAlt=%%D"
setlocal enabledelayedexpansion
for %%E in ("!progAlt:%%~nB=!") do (
endlocal
echo move /y "%%D" "%%~nxA%%~E"
)
)
)
I have removed almost all the inner variables that are simply using the values that the for replaceable parameters already hold, and used the adecuated modifiers to retrieve the needed part from the file names.

Batch only show directorys and their subdirectorys

I am having an question
How can i loop through an folder and only show folders and their subfolders without the full path.
example
if i use dir /b /s /ad
i will see e:\Mainfolder\Folder1\Subfolder
And i only whant to see
Folder1\Subfolder
the reason i want it is so i can put it inside an txt field and then with another loop check if the folder/subfolder exist somewhere else if not then it needs to create it.
With kind regards,
Thomas de Vries
#echo off
setlocal enableextensions disabledelayedexpansion
set "startingFolder=%cd%"
:: Determine the length of the starting path to remove
:: it from output
for /d %%a in ("%startingFolder%\"
) do for /f "skip=1 tokens=1 delims=:" %%b in (
'(echo(%%~fa^&echo(^)^|findstr /o "^"'
) do set /a "cutPoint=%%b-3"
:: Recurse folders from starting point and echo the
:: full path without the starting folder
for /r "%startingFolder%" /d %%a in (*) do (
set "line=%%a"
setlocal enabledelayedexpansion
echo(!line:~%cutPoint%!
endlocal
)
This will recurse over the tree structure, starting at the indicated folder (change startingFolder variable to what you need). For each folder found its relative path is echoed to console. Redirect output of batch to generate the required .txt file
#echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%a in ('dir /b /s /ad') do (
set "name=%%a"
echo !name:*e:\Mainfolder\=!
)

Resources