I have almost the final command line, but I need the rename part of it, to reach the goal I want I have this:
for /f “tokens=*” %a in (‘dir /b /s /a-d’) do #copy “%a” “C:\YourFolder” /y
and it works fine, but in my case I have tons of folders with only one file on each one, this file has the same name file.ext, so, is there any way to move and change the name, for example like file1.ext, file2.ext,...
Thanks!
This is not foolproof but it's likely to work, and will prompt if the small likelihood occurs that a filename exists. You'll need a batch file to make it foolproof and to improve the naming strategy.
Remove the #echo if you are happy with the result.
cmd /v:on /c for /f "delims=" %a in ('dir /b /s /a-d') do #echo copy “%a” “C:\YourFolder\%~na-!random!!random!!random!%~xa” /-y
Related
I have 2 files in my folder. One is "TEST ONE.txt", the other is "LEARN NEW.vcd".
I need to create a batch script to rename the *.txt using the *.vcd.
So the Batch script should rename the "TEST ONE.txt file" to "LEARN NEW.txt".
I have only one file with txt and one file with vcd extension in my folder C:\DEV folder.
I tried using the following script but it does not pick up the full name of the vcd extension and drops everything after the spaces - so I just get LEARN.txt :(
#echo ON
CD "C:\DEV folder"
setlocal enabledelayedexpansion
for /F %%A in ('dir /b *.vcd') do (
set xname= %%~nA
ren *.txt !xname!.txt
)
Thanks - db
It appears that the default in FOR /F may be to only take one (1) token. You can force it to take all using tokens=*. Quoting is important.
#echo ON
CD "sub"
setlocal enabledelayedexpansion
for /F "usebackq tokens=*" %%A in (`dir /b *.vcd`) do (
set xname=%%~nA
ren "*.txt" "!xname!.txt"
)
Just in case someone stumbles across this the way I did, there is a more elegant solution that I was forced to devise because I wanted it to go through multiple sub-directories, rather than run it one directory at a time. It is:
forfiles /S /M *.vcd /C "cmd /c rename *.txt #fname.txt"
Use your file extensions of choice.
I need it to work on Win10 and Win7 machines. If I can get this to work I'll make a batch file.
Winkey, "cmd"
cd "e:\media\trainingvids"
dir *.* /s /b /a -d > c:\temp\dork.txt
So, to state the obvious but make sure I'm getting it, I'm opening a command prompt, changing to the correct directory, doing a directory listing of all files (including sub-directories (/s), no headers or footers so 'bare' format (/b), and trying to NOT display the directory (/a -d) – and then sending/piping that (>) to a file I've designated to be named and created (dork.txt) in a temporary directory (\temp) that already exists on my c:.
The problem is that it doesn't work. I'm not able to find a way to NOT include the full path along with the file names. I need a nudge with the syntax. Or maybe I've got it all wrong and it can't be done in this way.
What does my Basic batch file look like that can do this?
You will need the for /F command to accomplish this:
> "D:\temp\dork.txt" (
for /F "eol=| delims=" %%F in ('
dir /B /S /A:-D "E:\media\trainingvids\*.*"
') do #(
echo(%%~nxF
)
)
You placed a SPACE between /A and -D in your dir command line, which must be removed.
Since I stated the full path to the target directory in the dir command and also to the output file, you can save this script at any location and run it from anywhere.
for /f "delims=" %%a in ('dir /s/b/a-d') do echo %%~nxa
should accomplish that task.
If you can tolerate double quoted names this batch file works well.
set myPath=c:\temp
set myMask=*.pdf
set myLog=c:\temp\myLogFile.log
FORFILES /P %myPath% /S /M %myMask% /C "CMD /C ECHO #file >> %myLog%"
Alter the values to meet your needs.
You're almost there with:
dir /s /w /a-d | find "."
The only drawback is that you get the file names in columns, and possibly a Volume line which you can remove with another find filter.
I'm trying to figure out how to match folders with a dot in the file name (e.g., ".svn") in a Windows batch script.
Here's the basic script:
setlocal
pushd c:\myDir
#echo off
FOR /D /r %%G in ("*\.whatever") DO (
echo %%G
REM do stuff
)
#echo on
popd
endlocal
This works just fine for most folder names (e.g., "*bin"), but I can't figure out the method to specify a folder with the dot. "*.whatever" and "*\.whatever" return no results. I'm guessing I'm missing some escape character or something equally simple, but I haven't been able to find any documentation on it.
(Before anyone asks, no I'm not trying to recursively delete subversion folders; "*.svn" is just an example.)
Maybe I am missing something, but as you say it seems simple
for /r /d %%a in (.*) do echo %%~fa
But if the folders are hidden, the normal for will not be able to see them, so we need to execute a dir command an process its output with a for /f
for /f "delims=" %%a in ('dir /ad /s /b .*') do echo %%~fa
I have a very simply batch file that I am trying to execute. The premise of it is simple: for every user, copy a folder and all its contents into another folder. However, upon executing I am unsuccessful. I do not get any error messages and it exits out immediately. I have ran the xcopy command itself with success, so this leads me to believe I am doing something wrong with the for loop. My knowledge with command prompt is relatively limited, so please pardon my ignorance in regards to the subject. Thanks for any help!
for /f "delims=" %a in ('dir /b /ad C:\Users') do xcopy C:\Folder "C:\Users\%a\AppData\Roaming\Folder" /f /j /s /w /y
It works for me from the command line (replacing xcopy with echo xcopy). From a batch file, though, you'll need to double your % signs, so make sure you're using %%a instead of %a.
I have directory structure:
DIR
|-UNINSTALL.BAT
|-component_name
|-source
|-setup.exe
|-uninst.bat
|-another_component_name
|-source
|-setup.exe
|-uninst.bat
|-yet_another_component_name
|-source
|-setup.exe
|-uninst.bat
and so on...
In every directory like "component_name" I have setup.exe file which installs current component to the palette component in Delphi.
uninst.bat contains only this string:
"setup.exe" /uninstall
So I need to write UNINSTALL_ALL.bat in DIR that would run the uninst.bat in all component directories.
Thank you in advance.
you could do it with this line:
for /f %%a in ('dir /b /s uninst.bat') do call %%a
note that the '%%' is necessary for batch files. if you are testing this on the command line, only use one '%'
That is kind of akward in a batch file. Though you could probably do it with the foreach statement. I would suggest though that you have a look at Powershell which will definitely give you the power to do this simply and a whole lot more if you want it.
You want to use the "for" construct. Something like this:
for %%i in (component_name another_component_name yet_another_component_name) do %%i\uninst.bat
The double-escaping (%%) is necessary if you put the "for" loop in a batch file. If you are just typing it out in a command prompt, only use 1 %.
Also, you may be able to use a wildcard to match against the directory names, if they follow some convention. Open up a command prompt and run "for /?" to see everything it can do...I believe there is a /d option to match against directories. That would look something like:
for /D %%d in (component_*) do %%d\uninst.bat
(obviously, adjust the wildcard to match your component directories.)
This should work:
FOR /F %%a IN ('dir /b /s uninst.bat') DO START /B %%a
if you want them to wait each other, use this:
FOR /F %%a IN ('dir /b /s uninst.bat') DO START /B /WAIT %%a
The way you describe your problem, you have only one level of sub-directories and you always call the same batch, from the root. Therefore:
Uninstall_all.cmd
#echo off
for /F "delims=" %%d in ('dir /b /ad') do cd "%%d"& start /b /w ..\uninstall.bat& cd ..
Should do the trick.