I got a script that lists all subdirectories in a folder and put them in a file:
dir "\\test\e$\1" /a:d /s /b | sort>"C:\folders.txt
effect looks like this:
\\test\e$\1
\\test\e$\1\target1
\\test\e$\1\target1\in
\\test\e$\1\target1\out
\\test\e$\1\target2
\\test\e$\1\target2\in
\\test\e$\1\target2\out
\\test\e$\1\target3
\\test\e$\1\target3\in
\\test\e$\1\target3\out
\\test\e$\2
\\test\e$\2\target1
\\test\e$\2\target1\in
\\test\e$\2\target1\out
\\test\e$\2\target2
\\test\e$\2\target2\in
\\test\e$\2\target2\out
\\test\e$\2\random_folder_without_in_subfolder
what I really need:
\\test\e$\1\target1
\\test\e$\1\target2
\\test\e$\1\target3
\\test\e$\2\target1
\\test\e$\2\target2
even better (if possible) in this form (separator: "|:"):
\\test\e$\1\target1|:\\test\e$\1\target2|:\\test\e$\1\target3|:\\test\e$\2\target1|:\\test\e$\2\target2
#ECHO OFF
SETLOCAL
SET "sourcedir=."
FOR /f "delims=" %%a IN ('dir /s /b /ad "%sourcedir%" '
) DO (
FOR /f "tokens=3,4delims=\" %%d IN ("%%a") DO IF "%%e"=="" (ECHO(%%a) ELSE GOTO secondway
)
:secondway
#ECHO off
SETLOCAL enabledelayedexpansion
SET "sourcedir=."
SET "longline="
FOR /f "delims=" %%a IN ('dir /s /b /ad "%sourcedir%" '
) DO (
FOR /f "tokens=3,4delims=\" %%d IN ("%%a") DO IF "%%e"=="" (
SET "longline=!longline!|:%%a"
) ELSE GOTO done2
)
:done2
SET "longline=!longline:~2!"
SET longline
FOR /f "tokens=1*delims==" %%a IN ('set longline') DO ECHO %%b
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
May have problems with directorynames containing characters that have a special meaning to cmd.
Related
I'm looking for a solution to my problem, below the script I'm using, I found it here, changed it, it was working the way I wanted to for what I was doing but now I want to do that and I'm struggling with the answer.
Example:
<br>Open_university_MS221_0001.tif</br>
<br>Open_university_MS221_0001-2.tif</br>
<br>Open_university_MS221_0002.tif</br>
<br>Open_university_MS221_0002-2.tif</br>
<br>etc.</br>
<br>Open_university_MS221_0001.tif</br>
<br>Open_university_MS221_0002.tif</br>
<br>Open_university_MS221_0003.tif</br>
<br>Open_university_MS221_0004.tif</br>
<br>etc.</br>
#echo off
setlocal enableextensions enabledelayedexpansion
set /a count=1
for %%f in (*.tif) do (
set FileName=%%~nf
set FileName=000!count!
set FileName=!FileName:~-4!
for /F "tokens=1-3 delims=_+" %%g in ('dir /b /od *.tif') do (
set prefix=%%g_%%h_%%i
)
set FileName=!prefix!_!Filename!%%~xf
rename "%%f" "!FileName!"
set /a count+=1
)
Working solution below, thanks to aschipfl who did show me the way and Stephan
#echo off
setlocal enableextensions enabledelayedexpansion
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose a folder.',0,0).self.path""
for /f "usebackq delims=" %%i in (`powershell %psCommand%`) do set "folder=%%i"
cd %folder%
set path=%folder:~1%
for %%i in ("%path%") do (
set "parent=%%~ni"
)
set /a count=10000
for /F "eol=| delims=" %%f in ('dir /B /A:-D-H-S /O:N "*.tif"') do (
set /a count+=1
set fileName=!parent!_!count:~-4!%%~xf
rename "%%f" "!fileName!"
)
exit /b
You can get rid of a lot of code within the loop, when you initialize the counter to 10000 before entering the loop. Then within the loop, you just use !count:~-4!
#echo off
setlocal enableextensions enabledelayedexpansion
set /a count=10000
for %%f in (*.tif) do (
for /F "tokens=1-3 delims=_+" %%g in ('dir /b /od *.tif') do (
set prefix=%%g_%%h_%%i
)
set /a count+=1
set FileName=!prefix!_!count:~-4!%%~xf
rename "%%f" "!FileName!"
)
The code could be shortened even more but at the cost of readability.
Oh - and I moved set /a count+=1 up to avoid starting with 0000 (your example shows, you don't want that)
I use the following script to keep only the newest 360 files (a year, daily-made backup) in the directory:
for /f "skip=360 eol=: delims=" %%F in ('dir /b /o-d /a-d *.*') do #del "%%F"
How to afterwards copy the newest 7 files to another directory?
#echo off
setlocal enableextensions disabledelayedexpansion
rem Three alternatives
rem Pure arithmetics
set "numFiles=7"
for /f "delims=" %%a in ('dir /b /o-d /a-d') do (
2>nul set /a "1/numFiles", "numFiles-=!!numFiles" && (
echo copy "%%~fa" x:\somewehere
)
)
rem Pure arithmetics 2 - No negation operator
set "numFiles=7"
for /f "delims=" %%a in ('dir /b /o-d /a-d') do (
2>nul set /a "1/numFiles", "numFiles-=1" && (
echo copy "%%~fa" x:\somewehere
)
)
rem Number list of files
set "numFiles=7"
for /f "tokens=1,* delims=:" %%a in ('
dir /b /o-d /a-d
^| findstr /n "^"
') do if %%a leq %numFiles% (
echo copy "%%~fb" x:\somewehere
)
I have the following batch script which gives the size of each folder in a directory. I need help in tweaking this or creating a new script so it gives the file count of each folders as well:
#echo off
setlocal disabledelayedexpansion
set "folder=%~1"
if not defined folder set "folder=%cd%"
for /d %%a in ("%folder%\*") do (
set "size=0"
for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\*" 2^>nul ^| findstr /b /c:" "') do if "%%~c"=="" set "size=%%~b"
setlocal enabledelayedexpansion
echo(%%~nxa # !size!
endlocal
)
endlocal
All the information is already in the output of the inner dir command, you only need to change what to retrieve
#echo off
setlocal disabledelayedexpansion
for %%a in ("%~f1.") do set "folder=%%~fa"
for /d %%f in ("%folder%\*") do (
set /a "size=0", "files=0", "directories=0"
for /f "tokens=1,3,5" %%a in ('
dir /-c /a /w /s "%%~ff\*" 2^>nul ^| findstr /b /c:" "
') do if "%%~c"=="" (
set "files=%%~a"
set "size=%%~b"
) else set /a "directories=%%~a/3"
setlocal enabledelayedexpansion
echo(%%~nxf # !size! bytes : !files! files : !directories! directories
endlocal
)
Add this cmd at the end and it will count files in a given dir
setlocal enableextensions
set count=0
for %%x in ("%folder%\*") do set /a count+=1
echo %count%
endlocal
This is inefficient tho, as you are traversing the whole directory twice.
Ideally, you need to combine the two FOR loops into one, just scan the dir once and add the size to the size counter and do count+=1 for each.
Try something like *added spacing to show what i added
set "folder=%~1"
if not defined folder set "folder=%cd%"
set count=0
for /d %%a in ("%folder%\*") do (
set "size=0"
for /f "tokens=3,5" %%b in ('dir /-c /a /w /s "%%~fa\*" 2^>nul ^| findstr /b /c:" "') do if "%%~c"=="" set "size=%%~b"
set /a count+=1
setlocal enabledelayedexpansion
echo(%%~nxa # !size!
endlocal
)
echo %count%
endlocal
Codes as below:
im getting below error:
FINDSTR: No search strings
I have traced the error an its coming from here:
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "!FILENAME!" ^| findstr /B "%numbers%"') do (...
Script is working properly if Im replacing FILENAME variable with exact filename manually. But I need to put it in a loop to execute within multiple files..
for /r %%i in (LOG_FILE*.txt) do (
set FILENAME=%%~nxi
for /F "delims=:" %%a in ('findstr /I /N /C:"fin.700 " !FILENAME!') do (
set /A val1=%%a-3, val2=%%a+3, val3=%%a+4, val4=%%a+11, val5=%%a+13 , val6=%%a+29, val7=%%a+30
set "numbers=!numbers!!val1!: !val2!: !val3!: !val4!: !val5!: !val6!: !val7!: "
)
set FILENAME=!FILENAME:~0,-1!
echo !FILENAME!>>tmptmptmp.tmp
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "!FILENAME!" ^| findstr /B "%numbers%"') do (
set linestr=%%b
echo !linestr!
)
)
Working without the outer FOR loop
#echo off
setlocal EnableDelayedExpansion
setlocal enableextensions
rem Assemble the list of line numbers
set numbers=
if exist "tmp" del "tmp"
if exist "tmp2" del "tmp2"
if exist "tmp.txt" del "tmp.txt"
REM for /r %%i in (LOG_FILE*.txt) do (
REM set FILENAME=%%~nxi
set FILENAME=LOG_FILE14012015.txt
for /F "delims=:" %%a in ('findstr /I /N /C:"fin.700 " !FILENAME!') do (
set /A val1=%%a-3, val2=%%a+3, val3=%%a+4, val4=%%a+11, val5=%%a+13 , val6=%%a+29, val7=%%a+30
set "numbers=!numbers!!val1!: !val2!: !val3!: !val4!: !val5!: !val6!: !val7!: "
)
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%FILENAME%" ^| findstr /B "%numbers%"') do (
set linestr=%%b
echo !linestr!
)
Thanks for the help guys. I did a workaround as im not able to force the findstr to work properly with my requirements. Did two separate batch script to handle the logic. Below is a copy in case anyone is interested.
bat file that will handle the outer for loop:
#echo off
setlocal EnableDelayedExpansion
setlocal enableextensions
::Script that will loop for multiple files and will call search.bat
if exist "tmp.txt" del "tmp.txt"
if exist "multiple_search.log" del "multiple_search.log"
#echo Starting search... >> multiple_search.log
for /r %%i in (LOG_FILE*.txt) do (
#echo Searching %%i >> multiple_search.log
call search.bat %%i
)
#echo Search completed... >> multiple_search.log
endlocal
Main bat file:
#echo off
setlocal EnableDelayedExpansion
setlocal enableextensions
set numbers=
if exist "tmp" del "tmp"
if exist "tmp2" del "tmp2"
set FILENAME=%1
for /F "delims=:" %%a in ('findstr /I /N /C:"fin.700 " !FILENAME!') do (
set /A val1=%%a-3, val2=%%a+3, val3=%%a+4, val4=%%a+11, val5=%%a+13 , val6=%%a+29, val7=%%a+30
set "numbers=!numbers!!val1!: !val2!: !val3!: !val4!: !val5!: !val6!: !val7!: "
)
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%FILENAME%" ^| findstr /B "%numbers%"') do (
set linestr=%%b
echo !linestr!
echo !linestr!>>tmp
)
set delim==
for /f "tokens=1*" %%a in (tmp) do (
echo %%a|find "!delim!" >nul
if !errorlevel!==0 (echo %%a%%b >> tmp2) else (echo record=%%a%%b >> tmp2)
)
set counter=0
set var=
for /f "tokens=1* delims==" %%a in (tmp2) do (
set /a counter=!counter!+1
set var=!var!%%b
set var=!var: =!
set var=!var!,
if !counter! geq 7 (
echo !var! >> tmp.txt
set counter=0
set var=)
)
endlocal
I am trying to count the lines of each text file in a directory on the condition that a text file with the same filename also exists in a second directory or one of its subdirectories. The script should count the lines of both files. Here's my code:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R "C:\Users\ABC\Documents\" %%W IN (*.txt) DO (
FOR /R "C:\Users\XYZ\Documents\" %%J IN ("%%~nxW") DO (
IF EXIST "%%J" (
set "firstfile=findstr /R /N "^^" "%%W" | find /C ":""
FOR /F %%G in ('!firstfile!') do set firstfilelines=%%G
set "secondfile=findstr /R /N "^^" "%%J" | find /C ":""
FOR /F %%H in ('!secondfile!') do set secondfilelines=%%H
ECHO %%W has !firstfilelines! lines.
ECHO %%J has !secondfilelines! lines.
)
)
)
pause
It counts the lines of text files in the first directory C:\Users\ABC\Documents\ but not in C:\Users\XYZ\Documents\ because findstr cannot recognize the value of %%J as a file path because it puts quotes around the filename as in C:\Users\XYZ\Documents\folder\"file.txt". How do I get rid of these quotes?
Use dir /s /b to build a list of files in the second folder
Use find /c /v "" filename to count the lines faster
Use set /a to trim the spaces in the assignment of the number
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
set "folder1=C:\Users\ABC\Documents\"
set "folder2=C:\Users\XYZ\Documents\"
FOR /R "%folder1%" %%W IN (*.txt) DO (
FOR /f "delims=" %%J in ('dir /s /b "%folder2%\%%~nxW"') DO (
IF EXIST "%%J" (
FOR /F "tokens=3 delims=:" %%G in ('find /c /v "" "%%W"') do set /a L1=%%G
FOR /F "tokens=3 delims=:" %%G in ('find /c /v "" "%%J"') do set /a L2=%%G
ECHO %%W has !L1! lines.
ECHO %%J has !L2! lines.
)
)
)
pause