this batch script to search file better
using "find" its just ordinary...and need to type exactly the file name "file 1.txt" "file2.txt" "file letter.txt"
and exactly folder location...
any other way?
just try type "file"
its show all file with name "file"
"file 1.txt"
"file2.txt"
"file letter.txt"
and have number
1 . "file 1.txt"
2 . "file2.txt"
3 . "file letter.txt"
and enter the number to select the file what we want...
and it will be open.
#echo off
setlocal EnableDelayedExpansion
if exist log del log
set "token=%~1"
if not defined token (
echo Search for what string?
set /p token=^>
echo.
)
set a=0
for /f "delims=" %%A in ('dir /b ^| find "%token%"') do (
set /a a+=1
echo !a!. %%A
echo !a!. %%A >>log
)
echo.
echo Enter number of file to open.
set /p op=^>
for /f "tokens=1,2 delims=." %%A in (log) do (
if %%A EQU %op% start %%B
)
del log
the problem is
when enter the number...batch otomatic close...file wont open...
any suggestion?
you might try this:
#echo off &setlocal
set /p "spat=Enter search pattern: "
for /f "tokens=1*delims=:" %%a in ('dir /b /s /a-d \%spat%*.txt 2^>nul^|findstr /ri "%spat%[^a-z]"^|findstr /rn $') do set "$%%a=%%~b"&set /a fcnt+=1
if not defined fcnt (echo Error! Not such file "%spat%".&goto:eof)
:loop
for /f "tokens=1*delims==$" %%a in ('set "$" 2^>nul') do echo %%a.%%~b
set /p "fno=Enter file number to run: "
echo %fno%|findstr "[1-9][0-9]*" >nul || (echo Error! Try again.&goto:loop)
if %fno% gtr %fcnt% (echo Error! Enter number between 1-%fcnt%. Try again.&goto:loop)
for /f "tokens=1*delims==$" %%a in ('set "$%fno%"') do "%%~b"
Related
I'm using windows bath, I have a list of names that I can add to but I don't know how to remove a name from the list.
So far my code is:
#echo off
setlocal enabledelayedexpansion
set allchoices=123456789
set "names=Bob,Steven,Harry"
set amount=6 ::max limit of list
set list=0
:start
::echoes a list of all names in the list
for /l %%i in (1; 1; %amount%) do (
call :sub %%i
)
goto check
:sub
for /f "tokens=%1 delims=," %%a in ("%names%") do (
echo %%i. %%a
set /a list=list+1
)
goto :eof
:check
::Remove a name from the list
choice /c !allchoices:~0,%list%! /m "What name do you want to remove?"
if errorlevel 3 (
for /f "tokens=3 delims=," %%a in ("%names%") do (
echo you have choosen to remove %%a
::remove third name in the list
goto start
)
)
if errorlevel 2 ::remove second name in the list
if errorlevel 1 ::remove first name in the list
I've tried using del but that turns out to delete a file in your folder.
I've tried renaming a specific name using set name[%%a]="" but that did nothing.
Any ideas?
Have a look at this example. There are many ways.
#echo off
setlocal enabledelayedexpansion
set names="Bob","Steven","Harry"
for %%i in (%names%) do (
set /a num+=1
set "!num!=%%~i"
)
for /l %%a in (1,1,%num%) do (
set choices=!choices!%%a
echo !num!.!%%a!
)
choice /c 123 /m "please select name to remove"
for /l %%a in (1,1,%num%) do if not "!%%a!"=="!%errorlevel%!" set new_names=!new_names! !%%~a!
echo %new_names:~1%
It can be done without the last for loop as well.. but I opted for it.
Here is some example code, for you to run, and then try to comprehend, I hope it helps rather than confuses:
#Echo Off
SetLocal EnableDelayedExpansion
For /F "Delims==" %%A In ('Set # 2^>NUL') Do Set "%%A="
Set "i=0"
For /F "Delims=:" %%A In ('FindStr "^::" "%~f0" 2^>NUL') Do (
Set /A i+=1
Set "#!i!=%%A"
Echo= !i!. %%A
)
:Ask
Set # 1>NUL 2>&1
If ErrorLevel 1 Echo= Empty names list&Timeout 3 1>NUL&Exit /B
Echo=&Set /P "Option= Choose a name to remove>"
Set #|Findstr "^#%Option%=" 1>NUL||GoTo :Ask
Set "Name=!#%Option%!"
Echo= You've chosen to remove "%Name%"
Timeout 2 1>NUL
Set "#%Option%="
ClS
For /F "Tokens=1*Delims=#=" %%A In ('Set # 2^>NUL') Do Echo= %%A. %%B
GoTo Ask
::Alan
::Beth
::Cleo
::Dale
::Eric
::Faye
::Greg
::Hugh
::Inga
Important note:Please ensure, before saving the above content as a Windows Command Script, that there is a line return, (blank line), at the end.
I am a newbie to Windows Scripting.
I am trying to list some txt files in several sub directories & want to copy a user selected file to a new destination. Please note that the file name is unique in different locations.
I got the first part to work (Listing out the files & locations) using the following script, but I am unable to copy the selected file to the new location.
#ECHO OFF
SET index=1
SETLOCAL ENABLEDELAYEDEXPANSION
SET FFPath="C:\Scripts - Backup Server\DKXpress_bkp"
SET NewPath=C:\DKServer
ECHO Recursively searching %FFPath%
echo.
FOR /F "delims=" %%f in ('DIR %FFPath%\*.txt /a:-d /s /b') DO (
SET file!index!=%%f
ECHO !index! - %%f
SET /A index=!index!+1
)
SETLOCAL DISABLEDELAYEDEXPANSION
SET /P selection="select file by number:"
SET file%selection% >nul 2>&1
IF ERRORLEVEL 1 (
ECHO invalid number selected
EXIT /B 1
)
SET NewFile=file%selection%
ECHO Copying %NewFile% to %NewPath%
ECHO.
COPY /Y "%NewFile%" "%NewPath%"
ECHO.
PAUSE
I think I am doing this part wrong
SET NewFile=file%selection%
Thank you all in advance
You don't need to set an index variable or delayed expansion, if you let Find do the work for you:
#Echo Off
Set "FFPath=C:\Scripts - Backup Server\DKXpress_bkp"
Set "NewPath=C:\DKServer"
Echo Recursively searching %FFPath%
Echo=
For /F "Delims==" %%A In ('"Set File[ 2>Nul"') Do Set "%%A="
For /F "Tokens=1* Delims=]" %%A In (
'"Dir /B/S/A-D-S-L "%FFPath%\*.txt" 2>Nul|Find /N /V """') Do (
Echo %%A] %%B
Set "File%%A]=%%B"
)
Echo=
Set /P "#=Select file by number: "
Echo=
For /F "Tokens=1* Delims==" %%A In ('"Set File[%#%] 2>Nul"') Do (
Echo Copying %%B to %NewPath%&Echo=
Copy /Y "%%B" "%NewPath%"
GoTo :End
)
Echo Invalid number selected
:End
Echo=
Pause
You need to use delayed expansion to get the file name assigned to the variable correctly.
SET NewFile=!file%selection%!
Remove the setlocal to disable delayed expansion.
You can try something like that :
#ECHO OFF
:Main
cls
SET index=1
SETLOCAL ENABLEDELAYEDEXPANSION
SET FFPath="C:\Scripts - Backup Server\DKXpress_bkp"
SET "NewPath=C:\DKServer"
ECHO Recursively searching %FFPath%
echo.
FOR /F "delims=" %%f in ('DIR %FFPath%\*.txt /a:-d /s /b') DO (
SET filepath[!index!]=%%f
ECHO [!index!] - %%~nxf - %%f
SET /A index=!index!+1
)
echo(
echo select file by number :
set /p Input=""
For /L %%i in (1,1,%index%) Do (
If "%INPUT%" EQU "%%i" (
ECHO Copying "!filepath[%%i]!" to "!NewPath!"
COPY /Y "!filepath[%%i]!" "!NewPath!"
)
)
echo Copying another file ? (Y = Yes or N = No) ?
set /p input2=""
If /I "!input2!"=="Y" (
goto :Main
) else (
goto :eof
)
I have a script that is designed to count the number of files with a value in the filename (****_1.jpg), compare it with the number of files with another name (****_2.jpg) and delete the larger number of files so the number of files are equal for each type.
This is what I've got so far
#echo off
setlocal enableextensions
set count1=0
set count2=0
for %%f in (.\seq\*_1.jpg) do set /a count1+=1
echo "1 " %count1%
for %%f in (.\seq\*_2.jpg) do set /a count2+=1
echo "2 " %count2%
if %count1% gtr %count2% (
set /a count=%count2%-%count1%
for /l %%A in (1,1,%count%) do echo "%count2% + %%A _1.jpg"
)
if %count2% gtr %count1% (
set /a count=%count2%-%count1%
for /l %%A in (1,1,%count%) do echo "%count1% + %%A _2.jpg"
)
endlocal
I can get the counts, I make it to my if statements and then nothing happens. What am I missing?
For now I'm trying to echo a list of files I'm about to delete.
In addition to the need for ENABLEDELAYEDEXPANSION, there really is no record of file names from either set. The "count" cannot be used to create the filename, can it?
I put in some code at the top to create a test set. Always referencing THEDIR obviates the need to hardcode a directory name in many places.
When you think it will work, remove echo from the DEL command line.
#echo off
setlocal enableextensions enabledelayedexpansion
set "THEDIR=.\seqtest"
if not exist "%THEDIR%" (mkdir "%THEDIR%")
for /l %%i in (1, 1, 5) do (echo %%i >"%THEDIR%\file_%%i_1.jpg")
for /l %%i in (1, 1, 7) do (echo %%i >"%THEDIR%\file_%%i_2.jpg")
set count1=0
set count2=0
for %%f in ("%THEDIR%\*_1.jpg") do set /a count1+=1
echo "1 " %count1%
for %%f in ("%THEDIR%\*_2.jpg") do set /a count2+=1
echo "2 " %count2%
if %count1% gtr %count2% (
set /a count=%count2%-%count1%
set /a "dcount=0"
for /f "usebackq tokens=*" %%f in (`dir /b "%THEDIR%\*_1.jpg"`) do (
echo DEL "%%~f"
set /a "dcount+=1"
if !dcount! EQU !count! (goto Outa2)
)
for /l %%A in (1,1,%count%) do echo "%count2% + %%A _1.jpg"
)
if %count2% gtr %count1% (
set /a count=%count2%-%count1%
set /a "dcount=0"
for /f "usebackq tokens=*" %%f in (`dir /b "%THEDIR%\*_2.jpg"`) do (
echo DEL "%%~f"
set /a "dcount+=1"
if !dcount! EQU !count! (goto Outa2)
)
)
:Outa2
:TheEnd
exit /b 0
If you want to delete any _1 file that does not exist as an _2 file and vice versa, you could loop over the _1 files and delete any for which no _2 file exists. Then, loop over the _2 files and delete any for which no _1 file exists.
for /f "usebackq tokens=*" %%f in (`dir /b "%THEDIR%\*_1.jpg"`) DO (
set "FN=%%~f"
set "BASE=!FN:~0,-6!"
if not exist "%THEDIR%\!BASE!_2.jpg" (echo DEL "%THEDIR%\!BASE!_1.jpg")
)
for /f "usebackq tokens=*" %%f in (`dir /b "%THEDIR%\*_2.jpg"`) DO (
set "FN=%%~f"
set "BASE=!FN:~0,-6!"
if not exist "%THEDIR%\!BASE!_1.jpg" (echo DEL "%THEDIR%\!BASE!_2.jpg")
)
As I understand this problem, you have two lists of files and you want to delete files from the larger list so both lists have the same number of files. Right? For example:
1_1.jpg 1_2.jpg
2_1.jpg 2_2.jpg
3_1.jpg 3_2.jpg
4_2.jpg
5_2.jpg
In previous example you want to delete 4_2.jpg and 5_2.jpg, correct? If the first list is the larger one, the files must be deleted from it; if both lists have the same number of files, no files be deleted. This solution do that:
#echo off
setlocal
rem Enter to the folder with files
cd seq
rem Get the first list of files
dir /B *_1.jpg > first.txt
rem Merge the first list...
< first.txt (
rem ... with the second list
for %%f in (*_2.jpg) (
rem For each file in second list, read a file from first list
set "first=" & set /P "first="
if not defined first ( rem The second list is larger: cut it
ECHO del "%%f"
)
)
rem If still are files in first list, it is larger: cut it
for /F "delims=" %%f in ('findstr "^"') do ECHO del "%%f"
)
I think, Your method for searching for all *.jpg files is little buggy. You should try this following approach instead. I've modified the code to check the code a little - but you'll get the idea.
#echo off
setlocal EnableDelayedExpansion enableextensions
set count1=0
set count2=0
for /f %%f in ('Dir /s /b "Photo\*.jpg"') do set /a count1+=1
echo "1 " %count1%
for /f %%f in ('Dir /s /b "Photo\*.Png"') do set /a count2+=1
echo "2 " %count2%
if %count1% gtr %count2% (
set /a count=%count2%-%count1%
for /l %%A in (1,1,%count%) do echo "!count2! + %%A _1.jpg"
)
if %count2% gtr %count1% (
set /a count=%count2%-%count1%
for /l %%A in (1,1,%count%) do echo "!count1! + %%A _2.jpg"
)
endlocal
pause >nul
You can change the searching path from - "Photo\*.jpg" to anything in both cases. And this one has given me desired result on the CMD. I Hope this will work fine for you.
...
echo "2 " %count2%
set /a count=count1-count2
set "count=%count:-=%"
if %count1% gtr %count2% (
...
You evidently want to calculate count and need a method to find its absolute value.
simply subtract one vale from the other, then replace any - in the result with nothing.
From the prompt, see set /? for documentation.
I tried to write a batch script that find all the paths of files that have the same name as the input string. right now it can find only the first file found, and i cant think of a way to make it list multiple files locations. I am not very experienced and I need some help.
this is part of the script code:
:start
cls
echo Enter file name with extension:
set /p filename=
echo Searching...
for %%a in (C D E F G H U W) do (
for /f "tokens=*" %%b in ('dir /s /b "%%a:\%filename%"') do (
set file=%%~nxb
set datapath=%%~dpb\
::the path of the file without the filename included "C:\folder\folder\"
set fullpath=%%b
::the path of the file with the filename included "C:\folder\folder\file"
goto break
)
)
:notfound
cls
echo Enter file name with extension:
echo %filename%
echo File Not Found!
ping localhost -n 4 >nul
goto start
:break
if "%datapath:~-1%"=="\" set datapath=%datapath:~,-1%
cls
echo 3 %filename% found
echo %fullpath1%
echo %fullpath2%
echo %fullpath3%
--- || ---
I want the script to search the computer and list every encountered files with the same name and I want to be able to put those files' paths into different variables.
For example, if readme.txt is the input, then I want the list of all the paths of all the files with that specific name (readme.txt) and I want to set variable for each path so I can use it after that.
input:
readme.txt
output:
3 files found
C:\folder\folder\readme.txt
C:\folder\folder\folder\readme.txt
D:\folder\readme.txt
#echo off
set filename=readme.txt
for %%a in (C D E F G H U W) do (
for /f "tokens=*" %%b in ('dir /s /b "%%a:\%filename%"') do (
echo you can do something here with %%~nxb in %%~dpb
echo full name: %%b
)
)
I see no need to set the filenames to variables, as you can process them inside your loop. But if you really need them (for some reason) in variables:
#echo off
setlocal enabledelayedexpansion
set filename=readme.txt
set count=0
for %%a in (C D E F G H U W) do (
for /f "tokens=*" %%b in ('dir /s /b "%%a:\%filename%" 2^>nul') do (
set /a count+=1
set _file[!count!]=%%b
)
)
set _file
You can try with this code :
#echo off
Title Searching for the path with the same file name
Mode con cols=80 lines=3 & Color 9E
SET /a Count=0
set /a cnt=1
set "FileName=Readme.txt"
set "Report=%~dp0Report.txt"
set "Folder2Copy=%~dp0Readme_Folder"
set "Result2Copy=%~dp0Result2Copy.txt
If exist %Folder2Copy% RD /S /Q %Folder2Copy%
If Exist %Report% Del %Report%
If Exist %Result2Copy% Del %Result2Copy%
echo(
Echo Searching for the path with the same file name
Rem Looking for fixed drives and store them into variables
SETLOCAL enabledelayedexpansion
For /f "skip=1" %%a IN ('wmic LOGICALDISK where driveType^=3 get deviceID') DO (
for /f "delims=" %%b in ("%%a") do (
SET /a "Count+=1"
set "Drive[!Count!]=%%b"
)
)
:Display
for /L %%i in (1,1,%Count%) do (
cls
Title Please wait a while ... Searching for "%FileName%" on "!Drive[%%i]!\"
echo(
echo Please wait a while ... Searching for "%FileName%" on "!Drive[%%i]!\"
Call :FindPathFile !Drive[%%i]!\ %FileName% >> %Report%
)
Start "" %Report%
Goto :AskQuestion
::***************************************************************************************
:FindPathFile <Location> <FileName>
Where.exe /r %1 %2
Goto :eof
::***************************************************************************************
:AskQuestion
cls & Mode con cols=100 lines=5
echo(
echo Did you want to make copy of all files found as name "%FileName%"
echo saved on "%Report%" ? (Y/N) ?
set /p "Input="
If /I "%INPUT%"=="Y" (
for /f "delims=" %%i in ('Type "%Report%"') do (
Call :MakeCopy "%%~i" "%Folder2Copy%\"
)
)
Call :Explorer "%Folder2Copy%\" & exit
If /I "%INPUT%"=="N" (
Exit
)
Goto :eof
::***************************************************************************************
:MakeCopy <Source> <Target>
If Not Exist "%~2\" MD "%~2\" (
if not exist "%2\%~n1" (
echo copying "%~1" to "%~2"
copy /N /B "%~1" "%~2" >>%Result2Copy% 2>&1
) else (
call :loop "%~1" "%~2"
)
)
::***************************************************************************************
:loop
set "fname=%2\%~n1(%cnt%)%~x1"
if exist "%fname%" set /a cnt+=1 && goto :loop
copy "%~1" "%fname%"
exit /b
::***************************************************************************************
:Explorer <file>
explorer.exe /e,/select,"%~1"
Goto :EOF
::***************************************************************************************
I wrote a very simple script to output the host machine's MAC addresses to a text file.
The script is exiting right after line 3 - 'IF DEFINED WRITEOK ('.
#echo off
cls
copy /Y NUL "%CD%\.writable" > NUL 2>&1 && set WRITEOK=1
IF DEFINED WRITEOK (
rem ---- we have write access ----
set DIR=%CD%\interfaces
set FILE=%DIR%\%USERNAME%.txt
IF NOT EXIST "%DIR%" (
MKDIR "%DIR%"
echo DIR '%DIR%' was created
) else (
echo DIR '%DIR%' already exists
) for /f "tokens=2 delims=:" %%i in ('ipconfig /all ^| findstr /i "Physical Host"') do (
echo %%i >> "%FILE%"
echo OUTPUT written to '%FILE%'
)
) else (
rem ---- we don't ----
echo DIR '%DIR%' is not writable
)
echo.
echo DONE!
pause
Try to put the FOR one line after the closing parenthesis :
...)
for /f "tokens=2 delims=:" %%i in ('ipconfig /all ^| findstr /i "Physical Host"') do (...
you can't start a FOR with a closing parenthesis in front :
This will not work :
(echo 1
) for /l %%a in (1,1,10) do echo %%a
and this will work :
(echo 1
)
for /l %%a in (1,1,10) do echo %%a
EDIT 1 :
For the path variables containing space use double quote :
"%cd%"
when using it.