I am currently trying to use batch to grab a specific file name and then place it in a variable since the file name may change but the "client" or "mui" portion will not.
These two lines are what I tried first. using dir to search a path and /b to grab only the name I then filter with "client" or "mui" and that works for finding just the name. But trying to pipe that into a variable didn't work.
dir "\\server\path\here" /b | Find "client" | set Client
dir "\server\path\here" /b | Find "mui" | set MUI
So I tried my hand at these other items I found during some googling but these didn't work either.
FOR "tokens=*" %%a in ('DIR "\\server\path\here\"' /b) do (SET OUTPUT=%%a)
for /f "tokens=*" %%i in ('dir \\\server\path\here /b | Find client') do #echo %%i
for /f %%a in ('dir \\server\path\here /B | find "client"') do set FileCount=%%a
What am I missing or doing wrong here?
For those who come here later I ended up using
for /f "tokens=*" %%i in ('dir \\server\path\here /b ^| Find "client"') do (Set BaseClient=%%i)
FOR "tokens=*" %%a in ('DIR "\\server\path\here\"' /b) do (SET OUTPUT=%%a)
should work (if you look for the only or the last file)
for /f "tokens=*" %%i in ('dir \\server\path\here /b ^| Find "client"') do #echo %%i
you forgot to quote the find string (maybe dir /b /a-d ... and find /i "client" might be a good idea), and the | has to be escaped.
for /f %%a in ('dir \\server\path\here /B ^| find "client"') do set FileCount=%%a
Filecount? You might want dir /b /a-d ... ^| find /i /c "client" here.
Related
I am pretty new at programming in batch files, and stack overflow users have being a lot of help. My problem is almost solved. Now there is only one thing that is missing to make my script work is.
for /f "tokens=1,* delims=: " %%i in ('type "C:\dev\1597\AssayInfo.txt" ^| findstr /i CouID') do set "number=%%j"
echo %number%
in this part of the code i need to be able to find the AssayInfo.txt without the folder 1597. In my case i will have a lot of folders with random generated numbers and all of them have a Assayinfo.txt, but if i try to run the code without the 1597 path it just say that could not find the file.
We kind of went through these already, but anyway:
To actually set it as a variable after file was found.:
#echo off
setlocal enabledelayedexpansion
for /f %%i in ('dir /s /b /a-d AssayInfo.txt') do (
for /f "tokens=2" %%a in ('type "%%~fi" ^| findstr /i "CouID"') do set "number=%%a"
echo Found number !number! in file "%%~fi"
)
Just another simple question and probably duplicate of Windows batch script to search for specific files to delete
But I just can't figure it out.
In a batch file, I have the following:
for /f "delims=" %%a in ('dir /a-d /s /b *.tmp ^| findstr /e /r c:"*\.tmp" ') do echo "%%a"
In a folder on which I run the script are the files test.tmp and test.tmpl. I want the script to echo just test.tmp, but it doesn't echo anything. Any help is appreciated.
because of /r, * is interpreted as "zero or more of previous char". What is the previous char? You don't need * here. Just findstr /e ".tmp". Another /i may be a good idea.
break>test.tmp
break>test.tmpx
break>test.xtmp
for /f "delims=" %%a in ('dir /a-d /s /b *.tmp^| findstr /ie ".tmp"') do echo "%%a"
Try this instead:
for /f "delims=" %%a in ('dir /a-d /s /b *.tmp ^| findstr /e ".tmp" ') do echo "%%a"
Do not use /R as it uses search strings as regular expressions.
Yeah, I've tried the most popular available solutions(1)(2)
They didn't help much; just restated what I already knew.
This works:
#echo on
set var=APPENDTEXT
for /f "delims=" %%a in ('dir *.* /b /a-d') do ren "%%a" "%%~na%var%%%~xa"
pause
but then I try to refine it a bit like so
#echo on
set var=APPENDTEXT
for /f "delims=" %%a in ('dir *.* /b /a-d | findstr /v /i "\.bat$" ') do ren "%%a" "%%~na%var%%%~xa"
pause
so that I do not end up renaming the batch file itself. But then everything got messed up.
I've tried several approaches for escaping, none working quite like I want them to.
Additional Information: From what I gather, escaping " inside findstr is a problem when it is itself inside something else. I've
tried escaping with "" and with /" and with ^" to no avail. Am I doing
something wrong in these approaches?
('dir . /b /a-d | findstr /v /i "".bat$"" ')
('dir . /b /a-d | findstr /v /i \".bat$\" ')
('dir . /b /a-d | findstr /v /i ^".bat$^" ')
What is the correct way to escape it?
*
What I want it to do ?
Simply put,
When I run this.bat file inside a folder, I want all the files inside
it to be renamed with a APPENDTEXT (except the bat file itself) Example:
a.dat --> aAPPENDTEXT.dat pleasework.txt --> pleaseworkAPPENDTEXT.txt
You have escaped the findstr statement correctly, but the pipe | symbol still needs to be escaped. | findstr → ^| findstr
#echo on
set var=APPENDTEXT
for /f "delims=" %%a in ('dir *.* /b /a-d ^| findstr /v /i "\.bat$" ') do ren "%%a" "%%~na%var%%%~xa"
pause
I'd like to ask your assistance in the following batch script:
- Search for a specific file in all drives
- If found extract an archive to that directory
Thank you in advance
Edit code added from the comments area
for /f "delims=" %%F in ('dir /b /s "C:\test.txt" 2^>nul') do set place1=%%F
for /f "delims=" %%F in ('dir /b /s "C:\copy.rar" 2^>nul') do set place2=%%F
unrar x place2\copy.rar *.gif place1
I have a question regarding deleting the oldest file after a folder gets an X amount of files with the same extension, in this case, all the files share in common the extension *.bak, they are small files that I create for my firefox RES, they have imprinted on the title, the date and hour of creation, and that's as far as I can go.
Anyways, I've stumbled across this: Batch Script to delete oldest folder in a given folder. And I'm struggling to get it to work on my idea.
The thing is that I want the batch to simply check which file is the oldest after it creates a new one using this simple line of code.
copy /y store.json "%DROPBOX%\RES BACKUPS\store.json %date:~-4,4%-%date:~-7,2%-%date:~-10,2%_%time:~0,2%hs-%time:~3,2%min-%time:~6,2%s.bak"
You can use this:
#echo off
for /f "delims=" %%a in ('dir /b /a-d /t:w /o:d "%DROPBOX%\RES BACKUPS\*.bak"') do (
del "%%a"
goto :breakLoop
)
:breakLoop
I suggest first testing it with echo del "%%a" to make sure it deletes the right file.
This works by getting the output of the dir command, which shows all files in bare format (only filenames, not sizes etc.) sorted by oldest files first. It then deletes the first file found, and breaks the loop.
A version that keeps deleting files while there are more than a specific amount:
#echo off
set "source=%DROPBOX%\RES BACKUPS\*.bak"
set "minFiles=5"
for /f %%A in ('dir "%source%" /a-d-s-h /b ^| find /v /c ""') do if %%A leq %minFiles% goto :eof
:loop
for /f "delims=" %%a in ('dir /b /a-d /t:w /o:d "%source%"') do (
del "%%a"
goto :breakLoop
)
:breakLoop
for /f %%A in ('dir "%source%" /a-d-s-h /b ^| find /v /c ""') do if %%A gtr %minFiles% goto :loop
pause
You can make this non-looping (but still only delete if there are more than 5) by removing the line for /f %%A in ('dir "%source%" /a-d-s-h /b ^| find /v /c ""') do if %%A gtr %minFiles% goto :loop