Appending a txt file from multiple CSVs in subdirectories - append

I am trying to write a batch file which will append all *.csv files in the immediate subdirectories to a single text file in the current directory.
From various sources I have managed to piece together this code which works fine for files in the current dir but not sub-dirs
for %%a in (*.csv) do (type %%a >> csvreport.txt)
If anybody could help me with this I would be extremely grateful as I have tried various approaches with wildcards but without success.

Yet another option...
for /f usebackq %%a in (`dir /s /b *.csv`) do (type %%a >> csvreport.txt)
EDIT: Reading your details a bit more ... you want just the immediate directories, you can do this:
for /f usebackq %%a in (`dir /b /ad`) do for %%b in ("%%a"\*.csv) do (type "%%b" >> csvreport.txt)

for /R .\ %%a in (*.csv) do (type %%a >> csvreport.txt)
The /R indicates recursive and the parameter afterward is the folder in which to start (.\ is the current directory).
You can find up more if you run for /?

dir /ad /b > dirs.txt
for /f "tokens=1*" %%i in (dirs.txt) do cd %%i & for %%b in (*.csv) do (type %%b >> c:\csvreport.txt) & cd ..
Using the /R flag will traverse all subdirectory trees. You can nest the 'for' statements to only work with the immediate subdirectories but not their subdirectories.

Related

Batch file to rename based on size on disk

I was going to make a batch script to share for ex-Dropbox account holders that will go through and rename a file OR folder if the size on disk is > 1 byte.
Using other forums I've managed to come up with the below.
#ECHO OFF
setlocal
set maxbytesize=1
FOR /F %%i IN ('PowerShell -ExecutionPolicy Bypass -File "FileSizeOnDisk.ps1" "E:\Desktop\Folder"') DO set SizeonDisk=%%i AND if %SizeonDisk% LSS %maxbytesize% (ren *.* *.*-DELETED)
EXIT
I just need help to loop the powershell command for every file, folder and files in a folder. I don't know how to properly place 2 commands after a DO and can't figure out the code to get it to work on multiple files.
#ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
for /f "delims=" %%o in ('dir /b /a-d "nameofdirectorytoscan\filemask"') do
FOR /F %%i IN ('PowerShell -ExecutionPolicy Bypass -File "FileSizeOnDisk.ps1" "nameofdirectorytoscan\%%o"') DO set "Jumba#%%o=%%i"
set size#
pause
The result is a set of variables named Jumba#nameoffile with values of filesize as returned from Powersmell.
You can then scan the list using
for /f "tokens=2,3delims=#=" %%b in ('set Jumba#') do echo name=%%b, size=%%c

Update file in each subdirectory with a specific name

I have a batch-file which pulls a file from a url using powershell and then outputs/updates the file in a specific directory. But I have many of these directories, the only thing that changes about the path is numbers between \command\ and \setup\. How would I get it to put the file in every folder automatically?
Essentially I would like to output the downloaded text file in each of the install subdirectories of that path.
Also how could I make it happen silently?
#echo off
echo !!! PRESS ANY KEY TO CONTINUE AND UPDATE!!!
pause
powershell -Command "Invoke-WebRequest http://example.com/log/read.txt" -OutFile C:\Users\Administrator\AppData\Roaming\base\command\234235234\setup\install\read.txt 2>NUL >NUL
echo !!! DONE NOW !!!
echo !!! YOU CAN RE-OPEN NOW !!!
for /d /r "dirname" %%a in (*) do if /i "%%~nxa"=="install" echo %%a
may be useful to you.
Your requirement is unclear. Do you want to copy the file to the install subdirectories of ...\234235234\.. only, or of ...\*\... ?
Replace dirname with the name of the starting directory, be it ...\234235234\.. or C:\Users\Administrator\AppData\Roaming\base\command and the command I have shown will report all of the install directories contained under dirname. All you need then do is to change the echo to an appropriate copy command - see copy /? from the prompt. You can suppress copy's responses by appending >nul 2>nul (suppress messages and suppress error messages)
for /d /r with * as the list element will process a list of all subdirectories starting at the nominated directory. The if command selects only the leaf directories that match install in either case (/i)
Since
for /d /r ...
does nor detect hidden directories, another approach is
for /f "delims=" %%a in ('dir /s /b /ad "dirname" ') do if /i "%%~nxa"=="sub1" echo %%a
Which in this case should be
for /f "delims=" %%a in ('dir /s /b /ad "C:\Users\Administrator\AppData\Roaming\base\command" ') do if /i "%%~nxa"=="sub1" echo %%a
The dir command produces a list in /b basic (name-only) form, /s including subdirectories, /ad of directories only (names with the directory attribute set). This list is processed line-by-line by for /f without delimiters so the entire line (including spaces, if any) is assigned to %%a and displayed.

Batch File to display file name and then certain lines in the files

I have a folder of .log files where the content of each file has multiple lines of the following format:
yyyy/mm/dd, hh:mm:ss, ComputerName, IPAddress, stuff, stuff
I would like to create a batch file to parse through the .log files and create the following output for any line in a file where ComputerName starts with "XPLT":
filename,yyyy/mm/dd,ComputerName,IPAddress
And preferably, I'd like to only look at files with a modified date within the last 30 days.
So far, I've only gotten the following code which doesn't even work and doesn't even include the file modified date and parsing by ComputerName. Looking for help because I've just not done this very much, and I can't find a good example online.
Echo EID,Date,PCName,IPAdd>CitrixLogs.csv
setlocal enabledelayedexpansion
for /f "tokens=1,3,4" %%i in ('dir /b "C:\LogFiles\*.log"') do (
echo %%i,%%j,%%k,%%l>>CitrixLogs.csv
)
I'd use a bit of FINDSTR magic:
set LOG_DIR=c:\logfiles
for /f "tokens=1-7 delims=:," %%L in ('%SystemRoot%\System32\findstr.exe /r /c:"^[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9],[0-9][0-9]:[0-9][0-9]:[0-9][0-9],xplt.*," %LOG_DIR%\*.log') do #echo %%L,%%M,%%N:%%O:%%P,%%Q,%%R,
`
REM
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "tokens=2-8delims=:, " %%a IN ('findstr /l /i /c:", XPLT" "%sourcedir%\*.log"') DO (
ECHO %%~nxa,%%b,%%c:%%d:%%e,%%f,%%g
)
GOTO :eof
redirect to your .csv as you will...
#echo off
setlocal enableextensions disabledelayedexpansion
:: configuration
set "logFolder=%cd%"
set "logFiles=*.log"
set "maxFileAge=30"
set "computerName=XPLT"
set "outputFile=CitrixLogs.csv"
:: adjust commands to execute according to configuration
set "ageFilter=robocopy "%logFolder%" "%logFolder%" "%logFiles%" /l /is /njh /njs /nc /ns /ndl /maxage:%maxFileAge%"
set "contentFilter=findstr /f:/ /i /r /c:"^^[^^,]*, [^^,]*, %computerName%" "
:: Generate output file
( echo(EID,Date,PCName,IPAdd
for /f "usebackq tokens=2,* delims=:" %%a in (
`cmd /q /c "for /f tokens^=* %%a in ('%ageFilter%') do echo(%%a" ^| %contentFilter% `
) do for /f "tokens=1,3,4 delims=," %%c in ("%%b") do echo(%%~nxa,%%c,%%d,%%e
) > "%outputFile%"
endlocal
This will use robocopy (or you can change it with forfiles) to search for files with a max age of 30 days in the indicated folder. Files will not be copied (/l) but the list will be echoed (the rest of the switch configure the output). This list of files is piped into findstr (/f:/) indicating where to search for the lines that match the indicated condition. This will generate an output with each line in the input file matching the condition, prefixed with the name (full name) of the file. This line is then splitted to output only the required fields.

How do I execute code on most directories except those listed in a file with a batch file?

So I am trying to execute code on most directories under another except for those that I list in a text file. So for example, if I wanted to run the code on all directories directly in C:\ but not say C:\avoidme\ I would add C:\avoidme as a line in exclusions.txt. But the code I have below does not seem to be working. Any ideas on how this could be done?
for /f %%d in ('dir /b C:\') do (
find /c "C:\%%d" exclusions.txt
if %errorlevel% equ 1 (
Do code here
)
#echo off
for /D %%d in (*) do (
%WINDIR%\system32\find "%%d" exclude.list >nul
if errorlevel 1 (
echo good %%d
) else (
echo bad %%d
)
)
The reason your code does not work is because the entire body of the FOR DO() clause is parsed at once prior to actually executing the FOR statement. But the value of %ERRORLEVEL% is expanded at parse time, so you never get to see the updated value at execution time.
You have additional issues
you should use the DIR /AD option to restrict the list to directories only
you aren't interested in seeing the output of your FIND command, so you should probably redirect the output to nul
gavendkoa has one solution that will work.
Another alternative is to use delayed expansion
setlocal enableDelayedExpansion
for /f %%d in ('dir /ad /b C:\') do (
find /c "C:\%%d" exclusions.txt >nul
if !errorlevel! equ 1 (
REM Do code here
)
)
This has a risk in that %%d will corrupt the value at expansion time if it contains !. This can be solved by toggling the delayed expansion within the loop.
Another alternative is to use the || operator, which means execute the following command if the previous command failed.
for /f %%d in ('dir /ad /b C:\') do (
find /c "C:\%%d" exclusions.txt >nul || (
REM Do code here
)
)
Perhaps the best option is to eliminate the need to worry about the errorlevel at all by piping the results of DIR directly to FINDSTR with the /V and /G: options to filter out values that appear in your exclude list.
The following is supposed to work
for /f %%d in ('dir /ad /b C:\ ^| findstr /l /v /g:"exclude.list"') do (
REM - Do code here
)
BUT - there is a nasty FINDSTR bug that can cause it to fail if you search for multiple literal strings of different lengths.
The fix is to force FINDSTR to use regular expressions instead, but then you need to escape any regular expression meta-characters that may appear in your exclusion list. For example, a directory named myName.ext would have to be escaped as myName\.ext.
for /f %%d in ('dir /ad /b C:\ ^| findstr /r /v /g:"exclude.list"') do (
REM - Do code here
)
Other characters that would need to be escaped within exclusion.list are \, ^, $, [, ]

How can i list all hidden files inside all subdirectories using batch scripting for windows XP?

dir /S /aH doesnt work as it wont delve any deeper inside of unhidden folders.
EDIT: turns out it WAS dir /S /aH just there wasnt any hidden or system files or folders within the non hidden files or folders i was testing on.
This is problematic and the only way I know to solve it is ugly and will give you the result in a "function":
#echo off
setlocal ENABLEEXTENSIONS
goto main
:EnumAllFiles
FOR /F "tokens=*" %%A IN ('dir /B /S /A:-D-H "%~1" 2^>nul') DO call :%2 "%%~A"
FOR /F "tokens=*" %%A IN ('dir /B /S /A:-DH "%~1" 2^>nul') DO call :%2 "%%~A"
goto :EOF
:mycallback
echo file=%~1
goto :EOF
:main
call :EnumAllFiles "c:\someDirToSearch" mycallback
(This does not tell the mycallback function about folders since you said you wanted files)
Edit: It seems like dir /B /S /a-D also works