So I have been searching in other topics how to rename files and folders in a .bat in a recursive way but it isn't working at all.
My code is:
# echo off
setlocal enabledelayedexpansion
set /p rut="Introduce folder: "
FOR /D /R %%x in ("%rut%"\*) DO (
cd %rut%
echo %cd%
pause
ren .\* "a"
)
exit
But this only renames the files that are on the first folder and not in the rest. Forfile won't work at all with the variables.
What I want to get is how I could rename everything inside the main folder (including subfolders) whatever it is, be it files or folders as "a" to solve the Windows problem of having routes way too long when trying to delete a full structure, that is why I can't use the ".txt" ".whatever" solution.
A powershell solution would be valid too!
Thank you very much
Edit I added a random to see if it was the name that was conflicting but no, it is still not working:
ren .\* "a%RANDOM%"
And renaming them from cmd works the same way, I mean, if I write ren "folder" "whatever" it will change but in the script doesn't work with "*"
Related
I am trying to make a little shortcut for my daily work. I often have to copy some files from let's say C:\folder0\folder1\aaaa\ to C:\folder0\folder1\bbbb\.
I want to create a batch file shortcut in the send-to menu. So I would first select the files and then click on the new added shortcut to the batch file which should do the rest.
#echo off
:here
if '%1'=='' goto exit
"C:\Program Files (x86)\Notepad++\notepad++.exe" "%1"
echo %cd%
shift
goto here
:exit
I started with opening the files in Notepad++ and displaying the path.
But I need a function that stores the path from the given files and changes the folder a to folder b. Afterwards it would take the new path for the standard copy function.
xcopy /s C:\source D:\target
I hope I could properly explain what I try to achieve.
I found a solution. This is my code and it is working for me now as long as there are no spaces in the path (has someone an idea to fix that?)
#echo off
:here
if '%1'=='' goto exit
set strpath=%cd%
set strresult=%strpath:folder1=folder2%
#echo The original file '%1'
#echo New path %strresult%
coyp /b/v/y "%1" "%strresult%"
shift
goto here
:exit
pause
I have a good command over cmd commands, but this may require a variable or a loop which is where I fail in batch commands. Please help if you can!
-- Have about 100 subdirectories each has 1-20 HTML files in it. There are about 100 HTML files in the root directory too.
-- Need to replace all HTML files in the above directories with the same HTML source (copy over existing file and keep the name the same). Basically trying to replace all existing files with a redirect script to a new server for direct bookmarked people. We are running a plain webserver without access to server-side redirects so trying to do this just by renaming the files (locked down corp environment).
Seems pretty simple. I can't get it to work with wildcards by copying the same file over to replace. I only get the first file replaced, but the rest of the files will fail. Any one with any advice?
This should do it from the command prompt. Replace % with %% for use in a batch file.
for /r "c:\base\folder" %a in (*.html) do copy /y "d:\redirect.html" "%a"
Without knowing more precisely how you want to update the file content I suggest the following rough approach.
To re-create your example, I had to create some folders. Run this command to do that:
for /l %i in (1,1,20) do mkdir fold%i
I then used this script to create some example files:
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do call :makefiles %%i
goto :EOF
:makefiles
set /a number+=1
touch %1\file%number%.txt
echo %number% >%1\file%number%.txt
I then used this script to append the text changed to the file. Not sure if that is what you wanted - probably you need something more sophisticated.
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do #for %%f in ("%%i\*.txt") do call :changetext %%f
goto :EOF
:changetext
echo changing file contents to ^"changed^" for file: %1
echo changed>>%1
we are OCRing all our sales order in our office. Pdf files being Created based on captured Data. The files looks like these S123456.pdf, S239463.pdf. some times the OCR software "reads" the "S" as a "5". (files end up created as 5123456.pdf.) I am looking for a Batch file for a windows command prompt environment that would rename only the files with the first character that starts with "5", rename it the "S" and leaving the rest intact. preferred to it apply to all sub-folders. I google around tried to modify some examples.. Can't get them working.. Please help!
A simple wildcard rename will actually do exactly what you want:
ren 5*.pdf S*.pdf
This should work - it will only echo the ren commands to the screen so if it looks ok then remove the echo
#echo off
setlocal enabledelayedexpansion
for /r %%a in (5*.pdf) do (
set "name=%%~nxa"
echo ren "%%a" "S!name:~1!
)
I'm very new to scripting in batch so please bear with me.
My goal is so as to move files which have the same filenames but with different extension; such as i want to move myfile.txt.1 and myfile.txt.2 without 'touching' myfile.txt
I've managed to use the wildcard * but it logically moves even the file which i don't want to move.(i.e. myfile.txt)
My question is... I was thinking of using a for loop to count files and using the "count" variable instead of the * , but is there a more direct way of implementing this script?
Attaching my script:
cd my_path
mkdir test
robocopy "src" "dest" "my_file.*"
echo The file was moved succesfully !!!!
I think robocopy should be able to handle your needs.
I would have expected the wildcard "my_file.txt.*" to work, but interestingly it still matches my_file.txt despite the lack of a trailing ..
But adding the /xf option to exclude the "undecorated" filename works for me:
robocopy "src" "dest" "my_file.txt.*" /xf "my_file.txt"
#ECHO OFF
SETLOCAL
SET sourcedir=c:\sourcedir
SET destdir=c:\destdir
FOR %%i IN ("%sourcedir%"\*.*) DO (
FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF EXIST "%destdir%\%%~nxi" (
ECHO CAN NOT MOVE "%%~fi" "%destdir%\%%~nxi"
) ELSE (ECHO MOVE "%%~fi" "%destdir%\%%~nxi")
)
GOTO :EOF
This should accomplish the task AAUI. It also detects the presence of a clashing filename in the destination directory. Simply remove the ECHO from the ECHO MOVE to activate after verifying your test. Personally, I'd leave the ECHO CAN NOT MOVE as-is to report a problem. Changing that to MOVE has the potential to overwrite an existing file.
I am currently trying to make creating par2 recovery files a little easier for myself. The following script goes through all the current-folder's subfolders and then creates par2 recovery files for all files in said subfolder.
FOR /R %%g IN (.) DO C:\WINDOWS\par2.exe c -r10 -s384000 "%%g\%%~ng.par2" "%%g\*"
del /q *.par2
pause
But now I have one issue, it uses the folder name as a filename for the par2 files but strips the "extension" like it would for a file. The folder names are something like "ConcertFootage1.avi_". That folder will then contain files like ConcertFootage1.avi.part01.rar and ConcertFootage1.avi.part02.rar and so on. Meaning I end up with par2 files like "ConcertFootage1.par2" where it should be "ConcertFootage1.avi.par2" in order to match the files in the folder correctly.
Now, even if it would not strip the "extension" from the foldername I'd still end up with "ConcertFootage1.avi_.par2" so I figured it would be best to just get the name of a file in the folder and use that as a base, meaning it would just strip the ".partX.rar" file-extension and that is exactly what I need.
I can't figure out how to do this in the "FOR DO" command though, can anyone help?
I understand your question, that you try to strip two extensions from a filename.
You can do it with two %~nX flags.
setlocal EnableDelayedExpansion
for /R %%g IN (.) DO (
set "filename=%%~g"
call :removeExtension result1 "!filename!"
call :removeExtension result2 "!result1!"
echo !filename! -- !result1! -- !result2!
)
exit /b
:removeExtension
set "%~1=%~n2"
exit /b