Jump to content

DOS Batch File


Recommended Posts

I would like to compress the contents of my current folder using a batch file, have the archive name be set to the current folder name, and place the archived file in the current folder.

I am now using the following script, however it places the archived file in the folder below, rather than in the current folder. Can someone correct my script please?

"C:\Program Files\WinRAR\RAR.exe" a "%cd%.rar"

Thank You

Link to comment
Share on other sites


I am now using the following script, however it places the archived file in the folder below, rather than in the current folder. Can someone correct my script please?

"C:\Program Files\WinRAR\RAR.exe" a "%cd%.rar"

Well, that's not really a script, it is a one-liner ;)

In 2K/XP and later you can use variable expansion:

http://www.robvanderwoude.com/ntfor.php

Try running this batch (save as varexpand.cmd):


@ECHO OFF
ECHO %0
ECHO d %~d0
ECHO dp %~dp0
ECHO dpn %~dpn0
ECHO dpnx %~dpnx0

But is seems to me like the snippet you posted has other problems, that one will create a .rar, but WHAT files would it add to it? :unsure:

jaclaz

Link to comment
Share on other sites

Here's a starter for you. (just drag and drop your source directory onto it).

RARMe.cmd

@"%ProgramFiles%\WinRAR\rar" a -r -ep1 "%~f1\%~nx1.rar" "%~f1\*"

If I correctly understood what you were attempting, this should be fine.

But there are situations where it may need tweaking, such as ensuring that the source directory doesn't already contain a previous archive of the same name.

Link to comment
Share on other sites

Thanks jaclaz, However that did not seem to work for me. Nevertheless I was able to figure out a solution, though I'm sure it is not the most efficient or elegant. Here is my solution. I'm not real good at this. Can it be improved on? Thanks again.

=============================================

REM Command Line Version

Call :FOLDER "%CD%"

EXIT

:FOLDER

Set MyCurDir=%~n1

"C:\Program Files\WinRAR\RAR.exe" a -m5 -av -r -t -dr "%MyCurDir%.rar"

===============================================

Link to comment
Share on other sites

Thanks jaclaz, However that did not seem to work for me. Nevertheless I was able to figure out a solution, though I'm sure it is not the most efficient or elegant. Here is my solution. I'm not real good at this. Can it be improved on? Thanks again.

You either completely fail to expose the problem or we completely fail to understand it.

Try again to explain what you are trying to do.

Please try to answer questions asked.

The snippet you originally posted contained NO filenames to add to the .rar, the second one as well doesn't, but you have added the -r switch.

RAR.EXE a -r defaults to adding to the archive all files it finds, is that what you want?

The effect of your batch may change depending on which is the current directory.

Additionally if the contents of %CD% contain a . (dot) your batch won't work.

How is the value to %CD% SET?

Post an example of:

SET CD

You don't need to specify the .rar extension with the a command, if I remmeber correctly.

jaclaz

Link to comment
Share on other sites

jaclaz , You are quite correct that it fails to work where a .Dot appears in the directory name. Let me try to elaborate on what I wish to accomplish - I want a batch file that is able to compress all the files in a directory with the resulting archive name being the same as the directory name which contains it. I chose rar instead of zip because of the support for extended file names within the archived file. No need exists to specify the files to compress because I want all *.* files to be added to the archive. The script I am using now accomplishes nearly everything, particularly in that it supports directory name spaces, but it still does not support a .Dot in the directory name as you wisely informed me. Can you please advise as to a solution?

========================= Begin z.bat ======================================================

Call :FOLDER "%CD%"

EXIT

:FOLDER

Set MyCurDir=%~n1

"C:\Program Files\WinRAR\RAR.exe" a -m5 -av -r -t -dr "%MyCurDir%.rar"

======================== End z.bat =========================================================

Sorry I cannot answer your question as to what "%CD%" does or how the value is set. I do not know. I really do not understand how this batch file works. I have not written batch files since DOS 5.0 days and find that I have forgotten nearly everything. This batch file is placed in my C:\Bin directory and that directory is specified in the path statement of the autoexec.bat file. I also do not understand how the effect of my current file will change depending on the directory structure I'm in, it seems to work ok here with the exception of .Dots in the directory names.

Thanks for your help

Link to comment
Share on other sites

What is wrong with the solution I've provided, which you've thus far ignored?

If it is that the produced archive is created inside the source folder as opposed to along side the batch file then simply change it to this!

RARMe_2.cmd

@"%ProgramFiles%\WinRAR\rar" a -r -ep1 "%~dp0%~nx1.rar" "%~f1\*"

Link to comment
Share on other sites

Wise Owl, Did not mean to ignore you. The solution you proposed (@"%ProgramFiles%\WinRAR\rar" a -r -ep1 "%~dp0%~nx1.rar" "%~f1\*")compresses the entire hard drive. I only want it to compress the files contained in the current directory I am in. I tried several variations and cannot make it work.

I need the resulting archived filename to match the current directory and support extended directory names, spaces in directory names, and .dots in directory names. The Archived file must end up in that same current directory. BTW, the -dr switch I use deletes all uncompressed files to the recycle bin after being archived, which I desire.

Any suggestions? Thanks

Link to comment
Share on other sites

Wise Owl, Did not mean to ignore you. The solution you proposed (@"%ProgramFiles%\WinRAR\rar" a -r -ep1 "%~dp0%~nx1.rar" "%~f1\*")compresses the entire hard drive. I only want it to compress the files contained in the current directory I am in. I tried several variations and cannot make it work.

I need the resulting archived filename to match the current directory and support extended directory names, spaces in directory names, and .dots in directory names. The Archived file must end up in that same current directory. BTW, the -dr switch I use deletes all uncompressed files to the recycle bin after being archived, which I desire.

Any suggestions? Thanks

You need to learn to follow instructions, I told you to drag and drop the source folder onto the batch file, it works flawlessly, there is no way it can do what you have stated unless you have failed to provide the source file parameter to the script.

You also need to explain what you mean by "current directory I am in". Is this the location in which the batch file is located or some other location. we need to know how you are running this batch file and from where!

To create the archive in the Current Directory, you shouldn't need to provide the location, it should be the default.

RARMe_3.cmd

@"%ProgramFiles%\WinRAR\rar" a -r -ep1 "%~nx1.rar" "%~f1\*"

In your batch file or console change to the directory you want as your current directory, then run the batch file like this:

PathTo\RARMe_3.cmd "D:\My Backups\This Week"

Where the quoted location is that of the source directory you wish to archive.

Link to comment
Share on other sites

Wise Owl. You have several good points. You are correct that I did not drag and drop. I'm still stuck in the long-gone past. I am executing my batch files either from a C: prompt in a dos box or from the old Norton Commander/Total Commander environment which handles everything via keystroke rather than mouse drag&drop. I was not aware that drag&drop functioned differently than DOS box execution . Wow, I'm getting too old. The fact is I never use Windows Explorer as it is much to slow for extensive file management when compared to Norton Commander or Total Commander, and am not very familiar with it. It did not even occur to me to use it when you suggested drag and drop.

In any case, I need to execute the batch file from a Dos window or from Norton Commander. Do you have any suggestions? Hope I did not offend you by not specifying this sooner, I just did not realize the changes that have occurred with time.

Link to comment
Share on other sites

Also, which OS are you running?

I had assumed 2K/XP or later, autoexec.bat is something that ONLY DOS/Win9x/Me uses.

If variable expansion works (your %~n1), you have a NT based system, and thus what you have in your autoexec.bat is completely irrelevant.

In NT systems %CD% is a "special" variable that means "Current Directory", and it's automatically set when you open a command prompt.

In case of using a UNC path, you need an additional step, see here:

http://www.robvanderwoude.com/clevertricks.php

What happens in your batch with %~nx1 ? ;)

jaclaz

Link to comment
Share on other sites

The Drag and Drop, (especially in your stated XP Pro x86), does not function any differently from the method you are using. Basically it is an automatic, (GUI), way of running the batch file with the dropped file as the parameter %1. The only way to archive your entire C: drive would be for you to have either provided it as THE REQUIRED PARAMETER or not to have provided a parameter at all. Since your example used a parameter in the same way, I presumed you were fully aware of this functionality.

Armed with this knowledge, try all three of my examples again and choose the one which suits your purpose.

The first example places the archive inside you source folder, the second along side the batch file and the third in the current directory.

Link to comment
Share on other sites

Wise Owl. If I use this portion of your code;

@"%ProgramFiles%\WinRAR\rar" a -r -ep1 "%~nx1.rar"

it archives the files in the correct directory, and places the archived file in that same directory, just as I desire. The only problem is that the resulting compressed archive filename is .rar with no prefix before the extension. I need the batch file to read the current directory name together with any spaces and dots in that directory name, and name the .rar file so that it matches the directory name, with the .rar extension.

Link to comment
Share on other sites

jaclaz, I just need the archive.rar filename to match the current directory name from a dos box. For example, in a dos box with z.bat as the batch file name and C:\dir1\dir2\directory contents.one as the current directory, I want to type z and press enter and have all the files in that directory compressed into a file named "directory contents.one.rar" for storage with the uncompressed files deleted. Notice that the -dr switch deletes the uncompressed files to recycle bin.

I am using WinXP. Thanks for the info that my autoexec.bat is useless. However, I seem to be successfully using it to add my C:\BIN directory into the path which seems to be created somewhere else.

First, I have no idea how or why my batch file works as it does, I'm totally unfamiliar with batch files in the recent operating systems. The batch file I'm using does that in all respects except for cases where a .Dot appears in the current directory name. I would like to fix this problem if possible. It seems to me that such a batch file would be quite useful to many people and I'm surprised that a simple google search does not turn one up.

I followed your suggestion and looked at the Clevertricks website, but it was either over my head, or I did not see what I needed. You asked me "What happens in your batch with %~nx1 ?" The answer is, I'm not sure, but it needs to be there. Given the current batch file as follows, let me step through what I see happening line by line:

========================== begin z.bat ============================================

Call :FOLDER "%CD%"

EXIT

:FOLDER

Set MyCurDir=%~n1

"C:\Program Files\WinRAR\RAR.exe" a -m5 -av -r -t -dr "%MyCurDir%.rar"

====================== end z.bat ==================================================

Line one (Call :FOLDER "%CD%")is required, or else my resulting archive file has no name in front of the .rar extension.

Line two (EXIT)is needed so that the whole compression routine does not duplicate itself and re-compress the files a second time. This effect mystifies me.

Line three (:FOLDER) is obvious as where to go.

Line four (Set MyCurDir=%~n1) is required, or else my resulting archive file has no name in front of the .rar extension.

Line five ("C:\Program Files\WinRAR\RAR.exe" a -m5 -av -r -t -dr "%MyCurDir%.rar") , I tried deleting line four and instead replacing "%MyCurDir%.rar" with "%~n1.rar", but it just results in an archive file with no name in front of the extension.

Sorry I cannot answer your questions better, but it is through my ignorance that I'm not giving you the information you need. Thanks for your efforts.

Link to comment
Share on other sites

Wise Owl. If I use this portion of your code;

@"%ProgramFiles%\WinRAR\rar" a -r -ep1 "%~nx1.rar"

it archives the files in the correct directory, and places the archived file in that same directory, just as I desire. The only problem is that the resulting compressed archive filename is .rar with no prefix before the extension. I need the batch file to read the current directory name together with any spaces and dots in that directory name, and name the .rar file so that it matches the directory name, with the .rar extension.

Well that tells me something which I would have ascertained earlier had you provide a response to jaclaz when asked in post number 11.

It appears that you may not have variable expansion working, try this:

  • In your console window launch a new window using this command
    START CMD /E:ON


  • In that window use the example I've provided, (Do not omit the "%~f1\*" from the end).
  • Then let us know what happens.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...