trouble with letters in .bat command to copy files from a dvd - batch-file

I created a .bat file to replace 3 files from a DVD burned into a directory on my PC, the directory in all others the path is fixed, however it has a complication in case of my driver cd player and dvd the letter is j. But in other drivers when inserted the dvd of course the letter will change to f or g or i for example, how to solve this? How exists? because the Type the folder where it will be copied this defined in problem, but how to get and copy all files of this folder specify in or add variants and search type let's assume that the driver is with the letter n as I put in bat to find this folder with those Files and paste inside the folder that I specified? Is it possible?
I have the following .bat: copy j:\dvd folder\*.* "c:\file of destination in the pc. This .bat file recognizes the drive J as if it were in any other PC in a universal way let's say, regardless of which PC the bat is fired it will go into that DVD folder will copy and paste in the specified folder in c: windows windows folder Is not the problem, the problem is fixed the problem is this other unit of dvd in which the letters of the unit change from there will not work when clicking this bat because it is in my pc for example in another it will be i, this is the problem.

You may be able to leverage the WMI command line:
#Echo Off
Set "CDROM="
For /F "Skip=1 Delims=" %%A In (
'"WMIC CDROM Where (MediaLoaded='TRUE') Get Drive 2>NUL"'
) Do For %%B In (%%A) Do If Exist "%CDROM%\DVD Folder\" Set "CDROM=%%B"
If Not Defined CDROM GoTo :EOF
Copy "%CDROM%\DVD Folder\*.*" "%SystemDrive%\Somewhere Existing"
Timeout -1

Think from another perspective.
#echo off
copy "%~Dp0Dvd Folder\*.*" "C:\Folder\"
rem We don't need \ in between %~Dp0 and Dvd Folder because %~Dp0 comes with a \
Place this batch file in your DVD root folder.
Explanation:
%~Dp0

Related

Renaming Multiple Files in Subdirectories

Long story short, I accidentally wiped my HDD the other day while trying to install a new SSD. I used a great program to undelete my partition; however, all the files were given an .efs extension. I am currently using:
:begin
RENAME *.extension.efs *.
I have tons of lines written for each file type, and this command works flawlessly. The only problem is that I have to manually paste this .bat into every folder and execute it in order for it to work.
Is there a way I can make it so when I run this .bat, it will go through all folders and subdirectories from a central directory? I'm anal about organization so all my music, albums, videos, TV shows, etc., are all in separate folders and it would take quite some time for me to run my original .bat from each.
Any help is appreciated!!
for /r /d %n in (.) do pushd "%n"&call "fullpathtoyourniftybatchfile"&popd
from the prompt will traverse the entire tree from wherever your current directory is.
You could also place your batch into any directory on your path and execute
for /r /d %n in (.) do pushd "%n"&call "yourniftybatchfilename"&popd
since windows searches the path for any executable it can't find in the current directory.
Here is a script that will rename files with extension efs in the current directory and all subfolders starting at the current directory. %%i is replaced by the full path to a folder or subfolders. Line 2 is needed because the for loop only does folders and subfolders.
#echo off
ren *.efs *.
for /f "tokens=* usebackq" %%i in (`dir/b/ad/s`) do (
cd %%i
ren *.efs *.
)

How can you use Windows CMD to save a copy of a file name to a blank text document?

I've been experimenting with Xcopy on my flash drive, which as the reader most likely knows, is a way to transfer multiple file copies very efficiently. During my experimentation, i had a thought. The code i ran(which will be disclosed at the end of the comment) was set to copy files from my desktop, music, photos etc. However, on my desktop was a particularly large file for a game i frequent. When the program got to the games files, it took an extremely long time to copy the files, as the game ran a 3D environment, and i needed this code to run quickly and efficiently. So, my thought was:Is there a way to copy the name of a file(in this case, the name of the game, FTB), have the program copy the name of the file only, then create a text file on the flash drive where the code is being run, then overwrite the name of that document with the copied name?
Here is the code being run, and i am aware that it has some obvious problems, but its being worked on:
#echo off
:: variables/min
SET odrive=%odrive:~0,2%
set backupcmd=xcopy /s /c /d /e /h /i /r /y /EXCLUDE:MyExcludes.txt
%backupcmd% "%USERPROFILE%\Pictures" "%drive%\all\My pics"
%backupcmd% "%USERPROFILE%\Favorites" "%drive%\all\Favorites"
%backupcmd% "%USERPROFILE%\Videos" "%drive%\all\Vids"
%backupcmd% "%USERPROFILE%\Documents" "%drive%\all\Docs"
%backupcmd% "%USERPROFILE%\OneDrive" "%drive%\all\Onedrive"
%backupcmd% "%USERPROFILE%\Desktop" "%drive%\all\Desktop"
%backupcmd% "%USERPROFILE%\Network" "%drive%\all\Other devices"
copy "Bookmarks.bak" "all"
removedrive D: -L -H -A -W:1000
if you need any extra information, just ask.
add the large file to the myexcludes.txt file so it wont copy
just create a new file on your backup drive using something like the following (assuming your file is called mylargefilesname.ext)
echo mylargefilesname.ext > "%drive%\location\mylargefilesname.ext"
------ edit ------
To do this for all .exe files you can exclude *.exe and then use a loop.
for /f %%f in ('dir /b "%USERPROFILE%\Pictures\*.exe"') do echo %%f > "%drive%\all\My pics\%%~nxf"
this is looping over the results of dir /b "%USERPROFILE%\Pictures\*.exe"
for each line of the output it will echo the full file path to your new folder using the filename + extension of the file (thats what the nx part does)
I know this is resurrecting an old thread, but I've found this code all over the internet (it's been copied a lot) with no clear explanation of the nonsensical %odrive% initialization and the non-initialization of %drive%.
It doesn't work because they left out some code. Here's how to fix it:
Replace:
SET odrive=%odrive:~0,2%
With:
SET odrive=%CD%
SET drive=%odrive:~0,2%
The first line gets the current directory as %odrive% and the second line strips out the first two characters (e.g., C:) and assigns them to %drive%
Note the script assumes it's being run from the removable drive. So you could simply replace all instances of %drive% with . and it would work just as well.

Batch file Drag and Drop multiple files from one folder to a new folder Error

So I have the batch file below.
#ECHO OFF
FOR %%I IN (.) DO SET FolderName=%%~nxI
MKDIR "C:\%FolderName%"
for %%i in (%*) do (
move "%%~i" "C:\%FolderName%"
)
So when I drag and drop multiple files into the batch file, it will take the name of the folder that holds the files that I drag and drop and make a new folder at C:\ with the same name and then move the files into the new folder at C:\
Example: The folder that holds the files that I want to move is name Shop. Then the folder Shop is located at
...\ground\bell\tower\Shop
Using the batch file will make a new folder name Shop at C:\
Example
C:\Shop
The batch files works when I drag and drop about 100 files at once. The problem is that when I drag and drop 300 files at once, it returns the Error that says "The filename or extension is too long". I am able to move the files manually so I know that it can not be cause by a filename being too long.
Then I made a new batch file thinking maybe it is a problem with the move command or my for loop. So I wrote the batch file below.
#echo off
FOR %%I IN (.) DO SET FolderName=%%~nxI
MKDIR "C:\%FolderName%"
MOVE "%cd%\*.*" "C:\%FolderName%"
Now the second batch file above works just about the same as the first batch file. Just that with the second batch file, I only need to drag and drop one file from the folder that I want to move and it will move all the files in the first folder to a new folder at C:\ even if the first folder had 1000 files.
My question is why does the first batch file fail if I drag and drop too many files at once. Using the second batch file work, so it can not be because of the move command or is it? Since I am moving all the files from one folder to the other, the second batch file fits my need and was wondering if there will be any problems with the second batch file or a better way of doing this.
When you Drag-n-Drop Files on you Bat File, your Bat is actually called as if you would have called it in your command prompt. So if you drag three files on your Bat i.e.
Testfile.txt
Testfile.md
Testfile.jpg
Then actually your Bat is called like this:
C:\MyBatch.BAT Testfile.txt Testfile.md Testfile.jpg
If you call more it is obviously something like:
C:\MyBatch.BAT Testfile.txt Testfile.md Testfile.jpg Testfile01.txt Testfile01.md Testfile01.jpg Testfile02.txt Testfile02.md Testfile02.jpg Testfile03.txt Testfile03.md Testfile03.jpg Testfile04.txt Testfile04.md Testfile04.jpg Testfile05.txt Testfile05.md Testfile05.jpg etc...
Ah, can you read the rest of the line? Ok, this is not as long as 1000 Files but get the point what the difference is in your scripts. Your command line buffer will not be able to capture that much input.
Actually the size of how many characters you can enter in you command prompt varies a bit from system to system, but there was something like 8k in winXP, i reckon it is still the same.
http://support.microsoft.com/kb/830473 <-- that could help more concerning max.
And since the length of your command prompt is so "short" you have to find different methods for longer file trails - as you did - you could also overcome this, if you output the files you want to copy in a text file and then use that as input for your copy bat.
dir /b >filelist.txt
so now that you have this file list, then you just read the text file line by line:
for /f "delims=" %%i in (filelist.txt) do echo D|xcopy "C:\FolderName\%%i" "c:\temp\%%i" /i /z /y
so this will actually read your file list.txt and will (for each filename in list) press D key and pipes it to the XCopy command.
So piping a filelist to your command is a much better way, where actually in your case, if you really want the whole dir and not just a selection, copying directories is faster than copying file by file.
Hope you Question is answered.
If I am not mistaken, you are trying to create a folder with the parent folder as name. So before you ask again the same question in another post, I will code what you are seeking.
#echo off
setlocal
set "destination=c:\destination"
for %%i in (%~1) do set "parent=%%~pi" &goto:next
:next
for %%i in ("%parent:~0,-1%") do set "parent=%%~nxi"
for %%i in (%*) do (
echo:
if not exist %destination%\%parent%\nul ( mkdir "%destination%\%parent%" )
move "%%~i" "%destination%%parent%"
)
In addition, a command with many arguments is not a problem in the previous code.
For example: Write a batch file with the following code
#echo off
setlocal
set "destination=c:\destination"
for %%i in (%~1) do set "parent=%%~pi" &goto:next
:next
for %%i in ("%parent:~0,-1%") do set "parent=%%~nxi"
for %%i in (%*) do (
echo:
if not exist %destination%\%parent%\nul (echo mkdir "%destination%\%parent%")
echo move "%%~i" "%destination%%parent%"
)
call it test-move.bat and put the following long command-line of 250 parameters in cmd:
test-move.bat TestFile001.txt TestFile002.txt TestFile003.txt TestFile004.txt TestFile005.txt TestFile006.txt TestFile007.txt TestFile008.txt TestFile009.txt TestFile010.txt TestFile011.txt TestFile012.txt TestFile013.txt TestFile014.txt TestFile015.txt TestFile016.txt TestFile017.txt TestFile018.txt TestFile019.txt TestFile020.txt TestFile021.txt TestFile022.txt TestFile023.txt TestFile024.txt TestFile025.txt TestFile026.txt TestFile027.txt TestFile028.txt TestFile029.txt TestFile030.txt TestFile031.txt TestFile032.txt TestFile033.txt TestFile034.txt TestFile035.txt TestFile036.txt TestFile037.txt TestFile038.txt TestFile039.txt TestFile040.txt TestFile041.txt TestFile042.txt TestFile043.txt TestFile044.txt TestFile045.txt TestFile046.txt TestFile047.txt TestFile048.txt TestFile049.txt TestFile050.txt TestFile051.txt TestFile052.txt TestFile053.txt TestFile054.txt TestFile055.txt TestFile056.txt TestFile057.txt TestFile058.txt TestFile059.txt TestFile060.txt TestFile061.txt TestFile062.txt TestFile063.txt TestFile064.txt TestFile065.txt TestFile066.txt TestFile067.txt TestFile068.txt TestFile069.txt TestFile070.txt TestFile071.txt TestFile072.txt TestFile073.txt TestFile074.txt TestFile075.txt TestFile076.txt TestFile077.txt TestFile078.txt TestFile079.txt TestFile080.txt TestFile081.txt TestFile082.txt TestFile083.txt TestFile084.txt TestFile085.txt TestFile086.txt TestFile087.txt TestFile088.txt TestFile089.txt TestFile090.txt TestFile091.txt TestFile092.txt TestFile093.txt TestFile094.txt TestFile095.txt TestFile096.txt TestFile097.txt TestFile098.txt TestFile099.txt TestFile100.txt TestFile101.txt TestFile102.txt TestFile103.txt TestFile104.txt TestFile105.txt TestFile106.txt TestFile107.txt TestFile108.txt TestFile109.txt TestFile110.txt TestFile111.txt TestFile112.txt TestFile113.txt TestFile114.txt TestFile115.txt TestFile116.txt TestFile117.txt TestFile118.txt TestFile119.txt TestFile120.txt TestFile121.txt TestFile122.txt TestFile123.txt TestFile124.txt TestFile125.txt TestFile126.txt TestFile127.txt TestFile128.txt TestFile129.txt TestFile130.txt TestFile131.txt TestFile132.txt TestFile133.txt TestFile134.txt TestFile135.txt TestFile136.txt TestFile137.txt TestFile138.txt TestFile139.txt TestFile140.txt TestFile141.txt TestFile142.txt TestFile143.txt TestFile144.txt TestFile145.txt TestFile146.txt TestFile147.txt TestFile148.txt TestFile149.txt TestFile150.txt TestFile151.txt TestFile152.txt TestFile153.txt TestFile154.txt TestFile155.txt TestFile156.txt TestFile157.txt TestFile158.txt TestFile159.txt TestFile160.txt TestFile161.txt TestFile162.txt TestFile163.txt TestFile164.txt TestFile165.txt TestFile166.txt TestFile167.txt TestFile168.txt TestFile169.txt TestFile170.txt TestFile171.txt TestFile172.txt TestFile173.txt TestFile174.txt TestFile175.txt TestFile176.txt TestFile177.txt TestFile178.txt TestFile179.txt TestFile180.txt TestFile181.txt TestFile182.txt TestFile183.txt TestFile184.txt TestFile185.txt TestFile186.txt TestFile187.txt TestFile188.txt TestFile189.txt TestFile190.txt TestFile191.txt TestFile192.txt TestFile193.txt TestFile194.txt TestFile195.txt TestFile196.txt TestFile197.txt TestFile198.txt TestFile199.txt TestFile200.txt TestFile201.txt TestFile202.txt TestFile203.txt TestFile204.txt TestFile205.txt TestFile206.txt TestFile207.txt TestFile208.txt TestFile209.txt TestFile210.txt TestFile211.txt TestFile212.txt TestFile213.txt TestFile214.txt TestFile215.txt TestFile216.txt TestFile217.txt TestFile218.txt TestFile219.txt TestFile220.txt TestFile221.txt TestFile222.txt TestFile223.txt TestFile224.txt TestFile225.txt TestFile226.txt TestFile227.txt TestFile228.txt TestFile229.txt TestFile230.txt TestFile231.txt TestFile232.txt TestFile233.txt TestFile234.txt TestFile235.txt TestFile236.txt TestFile237.txt TestFile238.txt TestFile239.txt TestFile240.txt TestFile241.txt TestFile242.txt TestFile243.txt TestFile244.txt TestFile245.txt TestFile246.txt TestFile247.txt TestFile248.txt TestFile249.txt TestFile250.txt

Windows batch file to copy files allow for renamed file extension but not copy duplicates

I want to copy files from two directories on a remote computer to a single directory on a windows server. In one directory I have files with the extension *.csv and in the other *.asc. Drives have been mapped with the UNC convention so we have drives such as Z:\ which are mapped to a specific folder on another computer. The scripts can reside in either the remote computer or on the windows server.
The files with the *.asc extension are then processed and then need to be renamed or moved to another directory (which our software does). The files with the *.csv extension are not changed
I tried using Robocopy and this worked with the MOVE switch
c:\scripts\ROBOCOPY.exe z:\ C:\files\Magellan /MOV /NP /R:2 /W:2 *.asc >c:\scripts\synchro.log
However, the source programme / software requires that these *.asc remain in the original source location - so I can`t use the move switch.
I don`t want to copy duplicate *.asc files - otherwise these files will be processed again.
So I need to only copy new files - ie files that have not been copied before and compare them to another location where the file has either been renamed or moved !
#echo off
pushd Z:\
for %%# in (*.csv) do (
echo n|copy /-Y "%%~f#" "C:\files\Magellan\"
)
for %%# in (*.asc) do (
echo n|copy /-Y "%%~f#" "C:\files\Magellan\%%~n#.csv"
)
try this.If your language settings are different than english you might need to change the echo n that applies to your language.Not sure if this can be done with robocopy or xcopy (I need to read their help in details).This will copy/move first the .csv files and then the .asc files . You can change the order and where is needed you can replace the copy command with move
Pick the order you want to run these lines in (and fix up the mapped drive letters). That said, I'm not sure that you just want to copy both csv and asc files to the Magellan directory as your question mentions comparing them to another location.
echo n|copy /-y z:\*.csv c:\files\Magellan
echo n|copy /-y y:\*.asc c:\files\Magellan\*.csv

I am trying to overwrite sticky keys(sethc.exe) with cmd.exe but when running the script it cant find files

I am trying to overwrite two files with each other but it gives me a message that it cant find the files specified.
#echo off
set /p Drive=Enter local hard drive letter:
if "%Drive%"=="" goto :eof
xcopy Drive:\windows\system32\sethc.exe Drive:
xcopy Drive:\windows\system32\cmd.exe Drive:\windows\system32\sethc.exe
Make sure you files are in the same path as your batch file is, for example:
#echo off
start minecraft.exe
In this example, obviously it starts minecraft, but in the format it currently is, it would require minecraft to be located on your desktop if your batch file is on your desktop, if minecraft.exe is in a folder named "Folder1" then you would type:
#echo off
Start "C:\Users\<your name>\Desktop\Folder1\minecraft.exe"
In order for this to work, your batch file will also need to be located in Folder1 as well. Obviously, instead of minecraft.exe, you would use the files that you are using.
As long as your batch file is in the same path as your files then it should work. I hope the example I used helped you.
You are using %drive% where you should use drive
and using drive where you should use %drive%

Resources