Multiple results of dir /b /s

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Lateralus138
Posts: 11
Joined: 17 Aug 2009 13:18
Location: Decatur, Il.
Contact:

Multiple results of dir /b /s

#1 Post by Lateralus138 » 30 Jun 2012 21:52

Hi all I have created a batch file to search my computer for a file and when found create a cabinet archive. My trouble is when multiple results are found it only archives one of the files and I imagine it is the last file found. My question is: is there a way to archive all results and put the multiple files into an archive? Here is the code below:

Code: Select all

@echo off
cd \
echo What file would you like to put into a cabinet?:
set /P query_input=
DIR /b /s %query_input% > temp.txt
set /p TOOLOUTPUT= < temp.txt
del temp.txt
set /P file_name=What would you like to name your archive?
makecab.exe "%TOOLOUTPUT%" "%userprofile%\Desktop\%file_name%.cab"
if exist %userprofile%\Desktop\%file_name%.cab goto a
if not exist %userprofile%\Desktop\%file_name%.cab goto b
:a
echo Your cabinet has been created sucessfully.
PING 1.1.1.1 -n 1 -w 3000 > nul
goto end
:b
echo Your file was not found and the archive has not been created
PING 1.1.1.1 -n 1 -w 3000 > nul
:end

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Multiple results of dir /b /s

#2 Post by Fawers » 30 Jun 2012 22:37

Hey there.
Try

Code: Select all

md tempFolder
for /f "delims=" %%a in (temp.txt) do copy /b "%%a" tempFolder
set /P file_name=What would you like to name your archive?
makecab.exe tempFolder\* "%userprofile%\Desktop\%file_name%.cab"
del temp.txt

instead of

Code: Select all

set /p TOOLOUTPUT= < temp.txt
del temp.txt
set /P file_name=What would you like to name your archive?
makecab.exe "%TOOLOUTPUT%" "%userprofile%\Desktop\%file_name%.cab"


I can't tell it's going to work since I'm not acquainted to "makecab.exe."

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Multiple results of dir /b /s

#3 Post by foxidrive » 30 Jun 2012 23:07

This works but while researching I found a mention that even moderately long filenames will generate an error. Perhaps paths too, I didn't test that.

When you enter the CAB filename don't enter .cab at the end.

Code: Select all

@echo off
set /P "query_input=What file would you like to put into a cabinet?: "
DIR /b /s "\%query_input%" > temp.txt 2>nul

if not exist temp.txt ( echo Your file was not found and the archive has not been created.&pause&goto :EOF)

set /P "file_name=What would you like to name your archive?: "

del temp.dir 2>nul
echo >> temp.dir .OPTION EXPLICIT
echo >> temp.dir .Set CabinetNameTemplate=temp.cab
echo >> temp.dir .Set Cabinet=on
echo >> temp.dir .Set Compress=on
echo.>> temp.dir 

for /f "delims=" %%a in (temp.txt) do (
echo >> temp.dir .Set DestinationDir="%%~dpa"
echo >> temp.dir "%%a"
echo.>> temp.dir 
)

makecab.exe /F temp.dir
del temp.txt
del temp.dir
del setup.rpt
del setup.inf

move "disk1\temp.cab" "%userprofile%\Desktop" >nul
ren "%userprofile%\Desktop\temp.cab" "%file_name%.cab"

rd disk1

if exist "%userprofile%\Desktop\%file_name%.cab" (
echo Your cabinet "%file_name%.cab" has been created sucessfully.
PING localhost -n 1 -w 3000 > nul
) else (
echo An error occurred: bang your head on the desk now.
PING localhost -n 1 -w 3000 > nul
)
pause


EDIT: changed two variables to include drive:\paths
Last edited by foxidrive on 30 Jun 2012 23:47, edited 1 time in total.

Lateralus138
Posts: 11
Joined: 17 Aug 2009 13:18
Location: Decatur, Il.
Contact:

Re: Multiple results of dir /b /s

#4 Post by Lateralus138 » 30 Jun 2012 23:33

@Fawers Thanx for the input but it did not work it resulted in this: ( I have cut out the first part because it is long and unimportant)

Code: Select all

        1 file(s) copied.
        1 file(s) copied.
        1 file(s) copied.
What would you like to name your archive?firefox
Cabinet Maker - Lossless Data Compression Tool

ERROR: Could not find file: tempFolder\*
Your file was not found and the archive has not been created.
Press any key to continue . . .

The folder "tempFolder" does exist in C:\ after this runs but only contains one version of the firefox.exe when I know there are at least 2 if not 3 versions of the file on my drive
@foxidrive Your code did not work either, it resulted in this:

Code: Select all

What file would you like to put into a cabinet?: firefox.exe
What would you like to name your archive?: firefox
Cabinet Maker - Lossless Data Compression Tool

Parsing directives (temp.dir: 1 lines)
temp.dir(7): ERROR: Could not find file: firefox.exe
temp.dir(10): ERROR: Could not find file: firefox.exe
temp.dir(13): ERROR: Could not find file: firefox.exe
ERROR: MakeCAB aborted: 3 errors encountered
Could Not Find C:\Users\Lateralus\Desktop\setup.rpt
Could Not Find C:\Users\Lateralus\Desktop\setup.inf
The system cannot find the file specified.
The system cannot find the file specified.
The system cannot find the file specified.
An error occurred: bang your head on the desk now.
Press any key to continue . . .

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Multiple results of dir /b /s

#5 Post by Fawers » 30 Jun 2012 23:38

What does the help for makecab show? Wouldn't you rather use winRar or 7zip?

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Multiple results of dir /b /s

#6 Post by foxidrive » 30 Jun 2012 23:43

It works here in Windows 7. I had three versions of firefox.exe in the cab file.

EDIT: Try it now. It had two variables with no path/drive info.

What file would you like to put into a cabinet?: firefox.exe
What would you like to name your archive?: firefox
Cabinet Maker - Lossless Data Compression Tool

2,741,664 bytes in 3 files
Total files: 3
Bytes before: 2,741,664
Bytes after: 950,585
After/Before: 34.67% compression
Time: 0.96 seconds ( 0 hr 0 min 0.96 sec)
Throughput: 2774.51 Kb/second
Your cabinet "firefox.cab" has been created sucessfully.
Press any key to continue . . .
Last edited by foxidrive on 30 Jun 2012 23:50, edited 1 time in total.

Lateralus138
Posts: 11
Joined: 17 Aug 2009 13:18
Location: Decatur, Il.
Contact:

Re: Multiple results of dir /b /s

#7 Post by Lateralus138 » 30 Jun 2012 23:49

Makecab /?:

Code: Select all

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\Lateralus>makecab /?
Cabinet Maker - Lossless Data Compression Tool

MAKECAB [/V[n]] [/D var=value ...] [/L dir] source [destination]
MAKECAB [/V[n]] [/D var=value ...] /F directive_file [...]

  source         File to compress.
  destination    File name to give compressed file.  If omitted, the
                 last character of the source file name is replaced
                 with an underscore (_) and used as the destination.
  /F directives  A file with MakeCAB directives (may be repeated). Refer to
                 Microsoft Cabinet SDK for information on directive_file.
  /D var=value   Defines variable with specified value.
  /L dir         Location to place destination (default is current directory).
  /V[n]          Verbosity level (1..3).

I thought about using other compression tools, I get the best compression out of 7zip with ultra, but I wasn't sure how or if the others could be used well with cmd prompt and batch. I am under the impression that Makecab is built especially for dos. Time for bed, I'll check back to this thread tommorow, thanx all for your efforts.

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Multiple results of dir /b /s

#8 Post by foxidrive » 30 Jun 2012 23:51

I edited it. Try it now.

Lateralus138
Posts: 11
Joined: 17 Aug 2009 13:18
Location: Decatur, Il.
Contact:

Re: Multiple results of dir /b /s

#9 Post by Lateralus138 » 01 Jul 2012 00:08

Excellent foxidrive, worked like a charm and keeps the folder tree locations so as to know which file is what and where it comes from, thanx a whole lot. Now tomorrow I'll come back and ask a few questions I have on what's doing what, but tonight i'm too tired. Thanx again!!!!

Post Reply