I have a script i am trying to alter to search multiple sub directories within a given path, then copy the files found to location create folder with the copied files in folder file was ran from. At the moment script works but does to search sub directories, How do i alter my script below please.
#echo off
set LIST= C:\batch\list.txt
set FILESPATH="C:\Test"
for %%i in ("%LIST%") do set DEST=%%~ni
for /F "usebackq delims==" %%i in (%LIST%) do (call :COPY_FILES "%%i")
:COPY_FILES
xcopy /qv %FILESPATH%\%1 .\%DEST%\*
I have tried to alter this line
for /F "usebackq delims=" %%i in ('dir /s /b "%LIST%"') do (call :COPY_FILES "%%i"), but gets an error that the file list.txt could not be found.
Thanks
Test this: it will only print the xcopy lines to the console.
If it seems right then remove the echo from the xcopy line and run it for real.
If the commands are not right then provide a sample of the lines to see which part is wrong.
#echo off
set "LIST=C:\batch\list.txt"
set "FILESPATH=C:\Test"
for /F "usebackq delims=" %%i in ("%LIST%") do (
echo xcopy /s/h/k/f/c/q/v "%FILESPATH%\%%i" "%%~ni\"
)
pause
Related
I tried using dir /b/s *.png to list the directories of all my png files. the results were like this:
D:\Newfolder\test\images\approved\11.png
D:\Newfolder\test\images\approved\12.png
D:\Newfolder\test\images\approved\13.png
D:\Newfolder\test\images\approved\14.png
D:\Newfolder\test\images\approved\15.png
D:\Newfolder\test\images\approved\16.png
D:\Newfolder\test\images\approved\17.png
D:\Newfolder\test\images\approved\18.png
D:\Newfolder\test\images\approved\19.png
D:\Newfolder\test\images\approved\20.png
I want it to be shortened so it will display only starting from the images folder;
images\approved\11.png
images\approved\12.png
images\approved\13.png
images\approved\14.png
images\approved\15.png
images\approved\16.png
images\approved\17.png
images\approved\18.png
images\approved\19.png
images\approved\20.png
is it possible to do?
if it's not, is there any way for me to edit the directories by deleting the first few folder from a generated text file?
say i put dir /b/s *.png > path.txt how do i edit the texts since the list got no whitespaces.
i'm still new to this so i'm not so familiar with much commands but this is as far as my understanding can do.
Assuming this comment in your question:
"I want it to be shortened so it will display only starting from the "images" folder;"
you always want from the images folder, and also assuming the images folder always exists in the path:
#echo off & setlocal enabledelayedexpansion
for /R %%i in (*.png) do (
set "_fp=%%~i"
echo !_fp:*\images\=images\!"
)
pause
Or to explicitly ensure you only include paths with \images\ in the path:
#echo off & setlocal enabledelayedexpansion
for /F "delims=" %%i in ('dir /b /s *.png ^| findstr \images\') do (
set "_fp=%%~i"
echo !_fp:*\images\=images\!"
)
pause
example of using pushd:
#echo off & setlocal enabledelayedexpansion
pushd "%~1"
for /F "delims=" %%i in ('dir /b /s *.png ^| findstr \images\') do (
set "_fp=%%~i"
echo !_fp:*\images\=images\!"
)
pause
popd
So the above you can run from cmd and simply pass the path you want the script to run in as script_name.cmd "C:\some path\here"
I have a directory with multipe levels of folders.
I am completely new to writing batch files and I am writing my first one.
Stuck for ages on trying to
find all files in the directory including sub-folder
get parent directory for each file
save as variable like %parent.filename%
I have been searching here:
https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490909(v=technet.10)
And on Google but unfortunately I am stuck.
So now I managed to save the full path of each file as variable, but I want %Folder.FileName% to return the parent directory only, not the full path.
This is the code I have been testing in the command prompt.
For /F %A in ('Dir Linkedin /A-D /s /b /o') do SET Folder.%~nxA=%~pA
EDIT
I also saw this thread
And tried this code:
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
But %~nxB doesn't return any value... I expected it to get the last string of the path.
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA. >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
Note the extra .
The path provided by the ~p modifier terminates in \ so adding . to this means "the directory name itself as though it was a filename"
As a one-line command (within a batch, decorated by standard palaver)
#ECHO OFF
SETLOCAL
FOR /F %%A in ('Dir test* /A-D /s /b /o') do FOR /F %%S in ("%%~pA.") do SET Parent.%%~nxA=%%~nS
set parent.
GOTO :EOF
I used the filemask test* to better suit my system.
I can't imagine you'd voluntarily perpetually re-type the command, so the format for use within a batch file is shown.
I would suggest you do this as a single nested For loop from the Command Prompt and with no output file:
For /F "Delims=" %A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do #For %B In ("%~pA.")Do #Set "Folder.%~nxA=%~nxB"
From a batch-file, perhaps this would help you out:
#Echo Off
Rem Remove any existing Folder. variables
For /F "Tokens=1*Delims==" %%A In ('Set Folder. 2^>NUL')Do Set "%%A="
Rem Set the new variables
For /F "Delims=" %%A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do For %%B In ("%%~pA.")Do Set "Folder.%%~nxA=%%~nxB"
Rem View any returned variables
Set Folder. 2>NUL&&Pause
In order to add a simple "hello" prefix to all pdf files in a folder I'm using this batch file:
FOR %%F IN (*.pdf) DO (RENAME "%%F" "hello%%F")
Saved this into a "rename.bat" file and placed it into the folder I need the files to be renamed. Then I just double click on "rename.bat".
This almost works but the 1st file gets the prefix added twice.
Let's say in the folder I have: A.pdf, B.pdf, C.pdf, they get converted into:
hellohelloA.pdf
helloB.pdf
helloC.pdf,
Do you know what's wrong in the batch file?
I noticed it always does this when files are more than one. It works ok when there is only one file in the folder, but it is not very useful :-).
/f removes the issue of recapturing an existing file:
FOR /f "delims=" %%F IN ('DIR /a-d /b *.pdf') DO (RENAME "%%F" "hello%%F")
#echo off
echo.
echo. Add Whatever Prefix...
echo.
echo. You Want To Add...
echo.
echo. To The Filename...
echo.
set /p variable=" > "
setlocal enabledelayedexpansion
for /f "delims=" %%a in (' dir /b /a-d *.pdf') do (
set oldName=%%a
Set newName=%variable%!oldName!
Ren "!oldName!" "!newName!"
)
exit
This works well..... Try It Out ... No Double Prefix... Ever.
I am using a batch file and need to be able to list all JavaScript files found at a relative path to where the batch script is run. The final list needs to be formatted as:
"C:\path\to\file1.js" "C:\path\to\file2.js" "C:\path\to\file3.js" etc..
So far I have done:
SET files=""
FOR /f "delims=" %%i IN ('dir /b /s ".\path\to\files\*.js"') DO (
SET files=!files! "%%i"
)
ECHO Files found: %files%
However, with the script above, all I get as output is:
Files found: "" C:\path\to\last\file.js
It only outputs the last file that was found in the directory. I don't know what the issue is here as to why it only appends the last file.
Run the Batch file below exactly as appears here:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "files="
FOR /f "delims=" %%i IN ('dir /b /s ".\path\to\files\*.js"') DO (
SET files=!files! "%%i"
)
ECHO Files found: %files%
If the output is not what you want, execute the next command directly from the command-line:
dir /b /s ".\path\to\files\*.js"
If previous line show the same files than the Batch file, then the problem is related to dir command.
I want to create a 0 byte file names dblank in a specific directory C:\Users\myUser\*.data\.
echo. 2>"C:\Users\myUser\*.data\dblank.txt"
The * sign in the above command refers to any letters or numbers. I do not know. How can I refer to any letters or numbers in my batch code?
Maybe this:
setlocal enableextensions
for /D %%i in (C:\Users\myUsers\*.data) do copy nul "%%~i\dblank.txt"
endlocal
You can omit setlocal/endlocal if command extensions are already enabled (cmd /E:on).
This works on every existing *.data folder, if any.
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data') do echo. 2>"%%f\dblank.txt"
EDIT
Filter results:
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data^|findstr /r "\\[0-9a-zA-Z]*\.data$"') do (
echo. 2>"%%f\dblank.txt"
)