Outputting contents of DIR command to file by specific extension - batch-file

I have the following command that I have in a BAT file:
dir /b /s /-p *.sas /o:n >"%CD%"\WIN_file_list.txt
The goal is to have a file that contains the full path of ONLY files with .sas extension.
The problem is that when I run the above script, it outputs everything with sas in the extension. The file contains all of the .sas files that I want, but also all of the .sasb7dat files that I do not want in the new txt file.
Any insight would be appreciated.
Thanks in advance.

Use findstr to filter:
dir /b /s /-p *.sas /o:n | findstr /E .sas >"%CD%"\WIN_file_list.txt

Related

Batch How to extract multiple zip files to their origin folder with 7zip?

I have multiple zip files in different folders and want to extract them into the folder they are in.
Parent Folder
-Folderwithzip1
--zip1
--[Content from zip1 should land here]
-Folderwithzip2
--zip2
--[Content from zip2 should land here]
I've tried
for /r "C:\zip" %f in (*.zip) do do 7z x -o "%~dpf" "%f"
I also tried
FOR /F "USEBACKQ tokens=*" %%F IN (`DIR /b *.zip`) DO ("C:\Program Files\7-Zip\7z.exe" e "%%F" -y)
but that does only extract the files in the folder where the batch file sits.
But the batch does not even start.
The command line contains twice the keyword do which is the first mistake.
The second mistake is the space character between -o and "%~dpf".
The help of 7-Zip is the file 7zip.chm in program files folder of 7-Zip which can be either opened from within 7-Zip or by double clicking on this file. There is on tab Contents the list item Command Line Version with the sublist item Switches containing the link to -o (set Output directory) switch. The syntax explained on this help page as well as the examples don't have a space between switch -o and directory path.
7-Zip also outputs an appropriate error message on execution with a space between -o and the output directory path:
Command Line Error:
Too short switch:
-o
So the solution is quite simple for the command line usage:
for /R "C:\zip" %I in (*.zip) do "C:\Program Files\7-Zip\7z.exe" x -o"%~dpI" -y -- "%I"
And for usage in a batch file with each percent sign escaped with one more percent sign:
for /R "C:\zip" %%I in (*.zip) do "C:\Program Files\7-Zip\7z.exe" x -o"%%~dpI" -y -- "%%I"
I suggest also running in a command prompt window for /? and read the help output for command FOR.
DIR is not needed here except there are also *.zip files with hidden attribute set which are always ignored by FOR or some of the ZIP files contain itself *.zip files and extracting those *.zip files should be avoided on running the batch file. Run in a command prompt window dir /? for help on command DIR.
The following command line can be used in a batch file to first run the command DIR in a separate command process started by FOR with using cmd /C, capture everything written by DIR to handle STDOUT and then process all captured lines (= full qualified file names) line by line:
for /F "delims=" %%I in ('dir "C:\zip\*.zip" /A-D /B /S 2^>nul') do "C:\Program Files\7-Zip\7z.exe" x -o"%%~dpI" -y -- "%%I"
DIR option /S is required here to let DIR search also in all subdirectories of C:\zip for *.zip files and get output the name of the found *.zip files with full path instead of file name only.

Robocopy command to copy multiple files but just the files not the dirs containing the files

I was researching but there's no answer about a command in Robocopy to do the following:
I want to copy in a single folder all *.log files (just the file not the directory that contain the file) from multiple folders in a single path.
The source tree is the folowing:
c:\QA\1\1.log
c:\QA\2\2.log
c:\QA\3\3.log
c:\QA\4\4.log
... and so on ...
e.g.
Source: c:\QA
Destination: c:\QA\LOG
File: *.log
I made this batch file but it also copies the folders which contain the files.
#echo
ROBOCOPY C:\QA\ C:\QA\LOG\ *.log /s
pause
OK, this is the solution, thanks to Chris Nathaniel
batch file:
#echo
for /R C:\QA\ %%f in (*.log) do copy %%f C:\QA\LOG\
pause
cmd.exe:
for /R C:\QA\ %f in (*.log) do copy %f C:\QA\LOG\

Run all programs file whose same name by batch file

I have a folder which contains many sub-folders, each this sub-folder contains a program file named bk.bat in its root.
I want to write a bat file so that I can execute all these bk.bat files.
Please help.
Mine is folder structure
Create a batch file in your WWW folder and put this code in it.
FOR /F "delims=" %%G IN ('dir /a-d /b /s bk.bat') DO CALL "%%~G"

how to copy files from one directory to other in batch command

I wish to copy a file with extension .dyn which is located in each subfolder of main folder(T15_finished). I wish to copy it into respective subfolder at other location(T15). I have created that location using xcopy command. Here, .dyn file is being copied successfully in respective subfolder in T15 folder(see below code). Now, I have a file which has extension as .dynain which is located in the same subfolder as .dyn. And .dynain file is also getting copied which i don't want.
Please see following code which i have created. Can anyone tell me whats wrong ?
#echo off
xcopy D:\Master\SPRINGBACK\CPW\T15_finished D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15 /t
xcopy /e D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15
pause
Short file names. If you do a dir /x in the folder containing the .dynain file, you will see the 8.3 filename generated for the file, and it will have .dyn extension.
If you know the extensions of the conflicting files, you can use robocopy with /xf switch to indicate the files (*.dynain) to exclude, or you can generate a exclude file to use with xcopy /exclude:file (see xcopy /? for a explanation)
Or, you can generate the list of files to exclude
(for /f "tokens=" %%a in (
'dir /s /b "D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn" ^| findstr /v /i /e /l ".dyn"'
) do #echo(%%~nxa)>excludedFiles.txt
xcopy /exclude:excludedFiles.txt /e D:\Master\SPRINGBACK\CPW\T15_finished\*.dyn D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15
Or (as posted by foxidrive), copy all and then delete the non needed files.
The short filename is being matched as well as the long filename. That is the reason.
A solution is to use another command to delete the files:
del /s "D:\Master\SPRINGBACK\FRESH_SPRINGBACK\CPW\T15\*.dynain"

Batch file to find path of files matching extension, then create new file in same directory

I'm trying to write a batch file that will find all files with a csproj extension in a large hierarchy of directories.
Wherever I find one, I want to then create a new SameName.csproj.user file, and put some hard coded text in it. i.e 'test text'
How can this be done in a batch script?
Not tested:
#echo off
:: put your dir here
pushd c:\cproj_dir
for /f "delims=" %%f in ('dir /b /s /a-d *.csproj') do (
echo test text >"%%~dpfnxf.user"
)

Resources