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"
)
Related
This is another problem that i got in the same code, that i have already asked for help, so i am not sure if it is a duplicate or not.
The problem now is when i run the script, at the command xcopy i wanted to be able to copy the whole folder that the file is located instead of only the file.
#echo off
setlocal enabledelayedexpansion
set /p COU1=COU No. 1 serial number?:
echo COU serial number is: %COU1%
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"
if !number!==!COU1! xcopy "%%~fi" "C:\dev\WORKING"
pause
)
so to simplify i want to copy the whole folder that the file "~fi" was found instead of only the file
i also tried something like this:
set /p COU1=COU No. 1 serial number?:
echo COU serial number is: %COU1%
for /D %%k in ("C:\dev\*") do for /f "tokens=1,* delims=: " %%i in ('type "%%k\AssayInfo.txt" ^| findstr /i CouID') do set "number=%%j"
if %number%==%COU1% (echo Hello it worked) else (echo ERROR:No files with that serial number)
pause
but the problem here it is the IF statment its outside the FOR, so it will keep changing the !number! even if it matches, and will take the last value found not the one that is equal to COU1.
we can use robocopy here. But you were trying to copy the filename, not the folder, remember we set !number! as the found value.
#echo off
setlocal enabledelayedexpansion
set /p COU1=COU No. 1 serial number?:
echo COU serial number is: %COU1%
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"
set "fpat=%%~dpi"
set "fpat=!fpat:~0,-1!"
for %%u in (!fpat!) do set "dst=%%~nu"
if "!number!"=="!COU1!" robocopy "!fpat!" "C:\dev\WORKING\!dst!" /E
pause
)
Had you provided all of the information you needed at the outset, you wouldn't have needed 4 questions to achieve this task.
Based upon what we now know, I would have suggested you perform the task like this:
#Echo Off
Set "COU1="
:Ask
Set /P "COU1=COU No. 1 serial number?: "
If Not Defined COU1 GoTo Ask
For /F "Delims=" %%A In ('FindStr /SMIC:"CouId: %COU1%" "AssayInfo.txt" 2^>Nul^
^|FindStr /VBLI "WORKING\\"') Do #For %%B In ("%%~dpA."
) Do XCopy "%%~B" "C:\dev\WORKING\%%~nxB" /ECIHRKY>Nul 2>&1
Based on the info you provided this script would be ran from C:\dev.
I am traversing folders on a drive, collecting file names with specific extensions, and building a string which is later used in a command line switch. When I find a qualifying file I need to know its full path as this is what is required by the command line. I currently use "%~dp0%%a\%%b" to build the full path, but I can see that may have limitations later on when the batch becomes more complex (e.g. it digs deeper into sub folders). I am hoping there is a way to replace "%~dp0%%a\%%b" with the path to the located file. Thank you:
#ECHO OFF
for /f "usebackq tokens=*" %%a in (`dir /b /a:d`) do (
pushd %%a
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%b in ('dir /b "*.E01" "*.L01" "*.AD1" 2^>nul') do (
SET EVIDENCE_STR=!EVIDENCE_STR! /e:"%~dp0%%a\%%b"
)
IF DEFINED EVIDENCE_STR (
ECHO !EVIDENCE_STR!
) ELSE (
ECHO No evidence files located in: %%a
)
endlocal
SET EVIDENCE_STR=
popd
)
PAUSE
Why do you need to create 2 loops, each running a dir command to find files? Why not just do for /R loop? Here is an example:
#echo off
set "files=*.E01 *.L01 *.AD"
for /R %%a in (%files%) do echo %%a
Simply use "Sub"-Option: /S of the DIR-Command:
Dir /B /S C:\*.jpg
cause it is that equivalent to:
#echo off
set "DirPath=C:\"
set "files=*.jpg"
for /R %DirPath% %%a in (%files%) do echo %%a
and so you should got the same result in each script.
The Problem: If you don't want any deep of SubDirectorys, you've to filter out these and so you may lose time - in both solutions.
I am new to batch scripting . I need to delete all files in a folder that DOES NOT contains some word in the file
found this code
#echo off
setlocal
pushd C:\Users\admin\Desktop\bat
findstr /ip /c:"importantWord" *.txt > results.txt
popd
endlocal
So how i can WHITE list this files, and delete all other?
Or i think there is easy way with just check if !contains and delete
but i don`t know how?
Supposedly, this problem could be solved in a very simple way combining these findstr switches: /V that show results when the search string is not found, and /M that show just the name of the files; that is:
#echo off
setlocal
cd C:\Users\admin\Desktop\bat
for /F "delims=" %%a in ('findstr /ipvm /c:"importantWord" *.txt') do del "%%a"
Unfortunately, the combination of /V and /M switches don't properly work: the result of /V is based on lines (not files), so a modification in the method is needed:
#echo off
setlocal
cd C:\Users\admin\Desktop\bat
rem Create an array with all files
for %%a in (*.txt) do set "file[%%a]=1"
rem Remove files to preserve from the array
for /F "delims=" %%a in ('findstr /ipm /c:"importantWord" *.txt') do set "file[%%a]="
rem Delete remaining files
for /F "tokens=2 delims=[]" %%a in ('set file[') do del "%%a"
This method is efficient, particularly with big files, because findstr command report just the name of the files and stop searching after the first string match.
#echo off
setlocal
set "targetdir=C:\Users\admin\Desktop\bat"
pushd %targetdir%
for /f "delims=" %%a in ('dir /b /a-d *.txt') do (
findstr /i /p /v /c:"importantWord" "%%a" >nul
if not errorlevel 1 echo del "%%a"
)
popd
endlocal
Not really sure what you want to do with /pfiles - files containing non-ansi characters appear to return errorlevel 1for these. if not errorlevel 1 will echo the files that do not contain the required string - remove the echo to actually delete the file(s)
This should work:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET "pathToFolder=C:\FolderToEmpty"
SET "wordToSearch=ImportantWord"
FOR /F "tokens=*" %%F IN ('dir %pathToFolder% /b *.txt') DO (
findstr /IP %wordToSearch% "%pathToFolder%\%%F">nul
IF !ERRORLEVEL!==1 (
DEL /Q "%pathToFolder%\%%F"
)
)
You will have to set the proper path to the folder you want to delete the files from and to replace ImportantWord with the substring you are looking for.
I try to use two for loops to get a simple job done:
The first for-loop brings me a list of files,
the second for-loop then should joind me the content of all files together in a tempfile (and later ftp it somewhere but thats not the problem).
So this is my code so far:
setlocal enableDelayedExpansion
REM FILE-MERGER
if exist "%temp%\ZS_aus_Files.csv" del /f /q "%temp%\ZS_aus_Files.csv"
for /f "delims=" %%x in ('dir /s /b /a-d C:\Documents\accounting\') do (
echo %%x
for /f tokens^=*^ delims^=^ eol^= %%f IN (%%x) DO echo %%f >> %temp%\ZS_aus_Files.csv
)
%%x so far has the complete path in it with spaces as well.
In the second for-loop (%%x) throws an error because of the space.
Output of my code is something like:
C:\Documents\accounting\file with spaces.csv
The file "C:\Documents\accounting\file" cannot be found.
Putting (%%x) in qoutes ("%%x") makes the output a filelist instead a merged content file.
I realy stuck here, hope someone can help me with that.
try with
if exist "%temp%\ZS_aus_Files.csv" del /f /q "%temp%\ZS_aus_Files.csv"
for /f "delims=" %%x in ('dir /s /b /a-d C:\Documents\accounting\') do (
echo %%x
for /f usebackq^ tokens^=*^ delims^=^ eol^= %%f IN ("%%x") DO echo %%f >> %temp%\ZS_aus_Files.csv
)
This is not documented by MS at all in for help but when usebackq is used with double quotes it can be used for file names and processes file names with spaces.
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