Read current drive of batch file and use drive name in command - batch-file

I have the following problem: We send customers a USB-stick with content that they need to import onto a Linux-computer via a import-function (no influence on the Linux-part, only on the USB-stick part). The Linux import-function cannot read files with spaces, i.e. the file "system volume information" which is created on every drive of a Windows computer disrupts the import function.
With a google research I found out, how I can delete this file on the drive of the USB-Stick. If I do that before disconnecting the USB-stick from my Windows computer, I can import the content on the Linux computer. I do this with a batch-file containing the following command:
rd /s /q D:\"system volume information"
However, those USB-Sticks are sent out to customers that will want to view the content on a Windows-computer as well, before importing it, i.e. the file "system volume information" is created anew. I would like to make it as simple as possible for the customer and put a batch-file on the stick where they simply have to double-click on it in order to delete the file. This is where my problem lies: I cannot be sure, that it will be drive D: where the stick will lie. This is why I am looking for a function or command that reads the drive of the batch-file (USB-Stick) and does the command of delete "system volume information". I read some things about %~d0 but I could not find out how I specifically have to write the command to make it work.
Would you be able to help me? I have very limited experience with batches, so I apologize if this question seems very easy for you.
Thanks
Milak

I have not tested the code:
#echo off
set P=%~d0
set "P=%P%\system volume information"
rd /s /q "%P%"

Related

Another RoboCopy File / Folder Migration Q

Hi Sorry to ask another question regarding robocopy but by its very nature it is quite diverse so I need to be clear on what I am trying to achieve.
Scenario.
Moving from a Nas to a windows file Share , Completely New ACL's will be created and inherited from the new share already setup . SO this is a straight file and folder copy with mirroring.
I decided to use powershell to run the command - it felt right at the time and achieved the desired result on a few shares .
I mapped the drive and ran the following .
robocopy z:\"Dept Folder 1"\ E:\Shares\deptdata\"New dept folder" /e /LOG+:file /v /TEE
robocopy z:\"Dept Folder 2"\ E:\Shares\deptdata\"New dept folder2" /e /LOG+:file /v /TEE
The files and folders copied as expected inheriting the new ACL's The first Job finished and then moved on to the second copy and again completed successfully.
What I would like to do now on some of the larger shares is incorporate the mir command as I need to keep a constant mirror on all folders until such time as I have tested the new ACL's and I have mapped the new drive via GPO.
My First Question knowing the behaviour when having batched commands is this .
When using the Mir command will the first command in the batch continue to mirror
the share and move onto the next Mirror command and so forth. or will it just sit their mirroring the first command and not move onto the second?
If it wont move on to the next command how do I achieve that is it possible? would i have to run multiple mirror commands at the same time and not batch them up in one?
When the Mir command is running if I add new files into the New destination will those files get deleted, or is it only mirroring the source and doesn't care about new files in the destination?
Where would I specify the MIR command?

Mapping Drive to an unknown share name?

I'm trying to create a batch file that can look through contents of network shares to find a specific named directory and subdirectory and then map a drive to the share that contains these. Problem being that, whilst the contents of the share have these folders in them to identify that this is the correct share, the share name itself may be different in different setups, so I can't just use the share name in the script.
In other words
\\server\share\sims\setups
the sims and setups directories will ALWAYS exist in the share, but the share name is the thing that can be different. The server name will already exist in the batch file as it is set by using the set /p command to prompt the user for the servers ip.
i have looked at the find and the forfiles commands but these seem to only work to search locally and not on network paths.
Thanks
As indicated by foxidrive you can use the net view command to list shares of a server.
The for command is able to iterate over output from commands. Combining these in the following rough commandline does map a drive for me if a certain folder/file is present.
for /F %a in ('net view \\srv') do if exist "\\srv\%a\sims\setup" net use z: \\srv\%a
(don't forget to change %a to %%a if you use this line in a command-script)
The net view does output some noise but that doesn't screw the result. You could add some extra handling to only handle shares after the '-----' line is received.
How does this work?
net view \\server outputs the following:
Shared resources at \\server
Awesome server
Share name Type Used as Comment
-----------------------------------------------------------------------------
Configuration Disk System Configuration
Download Disk Download Share (Full Access)
Public Disk Public Share (Full Access)
Blah Disk Blah share (Full Access)
The command completed successfully.
The for /f command parses each line up to the next space. From the FOR /?
FOR /F ["options"] %variable IN (`command`) DO command [command-parameters]
...you can use the FOR /F command to parse the output of a
command. You do this by making the file-set between the
parenthesis a back quoted string. It will be treated as a command
line, which is passed to a child CMD.EXE and the output is captured
into memory and parsed as if it was a file.
Using that technique would echo the following result if run on net view \\server:
Shared
Network
Share
-----------------------------------------------------------------------------
Configuration
Download
Public
Blah
The
The EXIST operator checks if a file/directory exists. By concatenating the servername, share and the expected folder you can find out if that folder does excist on that share.
As I said the first 4 lines and the last line are noise and should be ignored but they don't hurt too much in this scenario.

How to create Backup Batch file that copys only modified files & delete the dest files/folders that do not exist in the source?

I want to create a backup Batch file that meets these requirements:
Requirement:
1-Only copy the source files if the source file got modified.
2-If the destination contains the files/folders that do not exist in the source, then the destination files/folders will be deleted.
3-Copies all subdirectories, even if they are empty.
4-If something happened that make the coping process uncompleted (ie copied unsuccessfully), the program will revert & keep the old version of the destination folder.
5-The batch file should run at the time i turn off my PC or at 7pm Daily.
So, i tried this
xcopy C:\MyProject N:\backup\MyProject /V /Y /E /D
Note: /v=Verify, /y=No prompting /e=subdirs /d=Copy only changed
However, after running the batch, it did not delete the files/folders mentioned in requirement 2. Also, i don't know how to do the requirement 4 & 5.
I searched many questions but seem they didn't have the code to meet requirement 2 & 4 & 5.
I don't want to use backup tool in Win7 since it is very heavy & it requires a big backup disk space. I prefer something simple (why didn't Window make things simple?? ).
Backup is very important when u do the project, I hope my question will help a lot of other programmers.
Can anyone know a good solution for this?
If you want a mirror backup, which only copies new and changed files, removes files that no longer exist in the source - then Robocopy is built in and can do that. A batch file that is scheduled at 7pm daily will work.
Your requirement #4 is something that I haven't seen in any backup programs, including Robocopy.
I recently wrote a batch file like this for an old XP machine, since I didn't trust the format produced by NTbackup to be readable on future machines. E.g. Windows 7 and 8 require a download of some sort of converter: forget that. My batch file lives in the root C:\ directory, and I have a shortcut in my "tools" program list.
Write the file in Notepad, and save as e.g. c:\backup.bat. Run it from the "Run" command. The first time it runs, all the directories you specify will be copied exactly to your backup drive, including all subdirectories (I use a 4 Gbyte thumb drive: you may well need larger). The first time, my small 3 GB or so of stuff took about 4 minutes on an old XP machine.
Interestingly, the total uncompressed backup produced this way is no larger than the backup produced by NTbackup in Windows XP in that program's unreadable format.
Here's a batch file, with comments (REM)...
REM backup.bat
The xcopy line below is for stuff in directory c:\anydir and its subdirectories, being copied to a directory \anydir on an e: drive (e.g. USB drive).
REM The actual command:
xcopy c:\anydir e:\anydir /i /s /y /d
For other directories, just change the c:\directory and e:\directory names in the xcopy command. Watch out, however, for names like My Pictures, with a space: you must rename them (just delete the space), so that DOS recognizes them.
REM finish
exit
The /i means make any (sub)directories that don't exist on the backup drive. This therefore copies the whole branch from the c: directory where you start.
The /s copies source directories and subdirectories unless they are empty.
The /y says don't ask about over-writing existing files.
The /d says only copy files of a later date: perfect for backup, and very fast.
The files are exactly as original: no weird backup format.
With regard to the questioner's question #4, I think just running the backup again would overwrite any failure. But if that fails, wipe your backup drive and start over. And question #5...XP (and I think 7) allow scheduled tasks from the Startup menu: just insert c:\backup.bat in the list. But I haven't tried that.

Help with Batch Files?

What are batch files useful for? They just seem to be used to make viruses and other things...but it seems like shell scripting to me.
Whats the uses for batch files?
From Batch file article at Wikipedia:
Batch files are useful for running a
sequence of executables automatically
and are often used by system
administrators to automate tedious
processes. Unix-like operating
systems (such as Linux) have a similar
type of file called a shell script.
A simple example:
for /f "tokens=* delims=" %%i in ('dir /s /b /a:d *svn') do ( rd /s /q "%%i")
If you save the above line in a file called ClearSVNFolders.bat and after that execute a double click you'll delete every folder named svn that resides inside a root path...
You automated the whole process. You could easily spend hours doing the above task if you had a deep root directory, that is, one containing thousands of folders. :)
Batch files are the Windows equivalent of a Unix shell script. So you can use them to automate things.
You could use them for shell scripting. :-P
Of course, they kind of suck at that, compared to bash (or perl/python/tcl). But if you're on Windows, it's a one-horse race unless you want to install cygwin or msys and battle with Unix/Windows incompatibilities.
Batch Files are extremely useful. They are super easy to learn as well. you can make them do things on startup like say that a program wants to open itself and wont close even from taskman.exe you can force it to shutoff without warning.
or you could make games and ineractive things like i like to do.
i have a Messenger that i made with fully customizable colors and accounts with account management and servers.
But you probably dont trust me enough for you to download it.
But yea they are pretty useful.
Batch file is "a computer file containing a list of instructions to be carried out in turn."
We have been studying since childhood that computer is a dummy machine and this is a method of instructing a dummy machine.
For example :-
If you want to instruct the system to create a folder with random name then type ,
#echo off
md %random%
Creating Batch files enables you to execute several line of CMD commands in a single file.
For example :-
#echo off
md %random%
tasklist
Pause
The entire purpose of a Batch script is to execute several DOS commands in sequence:
echo Hello!
set var=7
echo I just made var=%var%!
pause
It was invented in MS-DOS for user simplicity to execute things they did all the time, the most notable thing being "AUTOEXEC.BAT" which started once the command interpreter started, people would add things like:
echo Welcome to my computer!
or
cd C:\Games\
To make it quicker to access their games or whatever they needed.

How do I use a batch copy to update files?

I need help writing a batch file to update templates on a database. Basically, all our clients have their own folder, with multiple templates inside. Due to the computer illiteracy of my office (sigh), there's no real better way to fix this. However, I need a way to update those templates in a batch. For instance
\\SERVER\New Client Template Folder\Correspondence\Transmittal Letter.WPD
is updated. I then need to copy it to:
\\SERVER\Client Files\Client 1\Correspondence;
\\SERVER\Client Files\Client 2\Correspondence;
...etc. Essentially, I need to copy to \\SERVER\Client Files\\*\\, and I need to make it a batch file that I can train someone else to use whenever I leave this job. How can I do that?
Thanks.
The new versions of Windows (7 and 2008 Server R2) have the robust file copy tool (robocopy). This can be installed on XP and 2003 also be installed using the Resource Kit. Essentially, robocopy gives you a command-line directory mirroring tool that could help you accomplish what you're trying to do. Simply place robocopy commands into a batch file (/MIR = mirror directory contents /XJ = ignore junctions) :
robocopy <source_dir> <destination_dir> /MIR /XJ
You didn't indicate which operating system you are working under. Let me guess its windows. My DOS BAT file knowledge is limited, but you
could try creating a BAT file with something like:
set Src="\\SERVER\New Client Template Folder\Correspondence\Transmittal Letter.WPD"
set DestA="\\SERVER\Client Files\
set DestB=\Correspondence;"
FOR /F "delims=" %%i IN (distribution.txt) DO copy %Src% %DestA%%%i%DestB%
and then create a distribution.txt file like:
Client 1
Client 2
Running this BAT file will read the distribution.txt file and issue a copy command for each line in it. As follows:
COPY "\\SERVER\New Client Template Folder\Correspondence\Transmittal Letter.WPD" "\\SERVER\Client Files\Client 1\Correspondence;"
COPY "\\SERVER\New Client Template Folder\Correspondence\Transmittal Letter.WPD" "\\SERVER\Client Files\Client 2\Correspondence;"
But there must be a better way!!!!
You can get more help on the FOR command by typing help for at the DOS prompt.
If you don't like the idea of having to build/maintain the distribution.txt file, you could play with using DIR /A:D /B "\\SERVER\Client Files\*" to drop a directory listing into a temporary file, then use it as input to the FOR loop.

Resources