I want to make lots of files 0 bytes on windows.
The command copy /y nul test.vtx was working. I need to change their size without changing the file names.
How can i use copy command to automatically detect the filenames and use them to erase its contents? Would be a bath file helpful?
Thanks.
With break> i don't know if it is possible anyway you can iterate :
for %G in (*.vtx) do (copy /Y nul "%G")
Including subfolders :
for /R %G in (*.vtx) do (copy /Y nul "%G")
Another way using PowerShell. When you are satisfied that the correct files will be overwritten, remove the -WhatIf from the Set-Content statement.
Get-ChildItem -Path C:\src\t\adir -Filter *.vtx |
ForEach-Object { Set-Content -Path $_ -Value $null -WhatIf}
Usage :: ROBOCOPY source destination [file [file]...] [options]
source :: Source Directory (drive:\path or \\server\share\path).
destination :: Destination Dir (drive:\path or \\server\share\path).
file :: File(s) to copy (names/wildcards: default is "*.*").
/CREATE :: CREATE directory tree and zero-length files only.
I see you specified "copy" but I assumed you or others might accept robocopy too. My guess would be:
ROBOCOPY C:\vtxfolder1 C:\vtxfolder2 *.vtx /CREATE
Related
How can I use windows bat or powershell to mass-copy files from child-dirs to the root/main directory? eg,
c:\dir\1
c:\dir\2
c:\dir\3
etc
to
c:\dir
without having to copy-paste them all manually.
In a batch file:
for /d %%d in ("C:\Dir\*") do for %%f in ("%%d\*") do copy "%%f" "%%d\.."
Interactively:
for /d %d in ("C:\Dir\*") do for %f in ("%d\*") do copy "%f" "%d\.."
Hint: for /d iterates over directories. Use for /? for more details.
If it's only the files you want to move then a quick a dirty method would be something like:
$InputFolder = "C:\Dir"
$FilesToMove = Get-ChildItem -LiteralPath $InputFolder -Recurse | where { ! $_.PSIsContainer }
foreach ($File in $FilesToMove)
{
Move-Item -LiteralPath $File.PSPath -Destination $InputFolder -Force
}
So if for example you have:
C:\Dir\MyDir1\File.txt it will move it to c:\dir\file.txt
However, no folders will be moved.
eg say c:\Dir\MyDir2\AnotherDir\ contains File1.txt and Fle2.txt only the .txt files will get moved, c:\Dir\MyDir2\AnotherDir\ would remain intact but without any files.
Expanding upon this example: Copying a file to multiple folders in the same directory I want to copy an XML file to several different project directories under the same root folder, so I have tried this:
for /d %a in (C:\Root\Output\*\bin\x64\Release) do copy /y C:\Root\OtherProject\MyXml.xml %a\
Where the wildcard would be a different project name in my solution directory.
When I run this command, there is no error, but the XML is not copied, so my question is can you use wildcards like this in Windows command line or alternatively is there a better way to tackle this kind of operation from within command prompt?
Just split it:
FOR /D %1 IN (c:\root\output\*) DO (
COPY /Y c:\root\otherproject\myxml.xml %1\bin\x64\release
)
Obviously this works if all subdirectories of c:\root\output must be included (they have a bin\x64\release subdirectory.) If it's not true then you have to include an extra check:
FOR /D %1 IN (c:\root\output\*) DO (
IF EXIST "%1\bin\x64\release" (
COPY /Y c:\root\otherproject\myxml.xml "%1\bin\x64\release"
)
)
Of course you may feel this is to much code for such simple task, well then maybe it's time to switch to PowerShell, Get-ChildItem supports wildcards used the way you want:
Get-ChildItem c:\root\output\*\bin\x64\release `
| ForEach-Object -Process `
{ Copy-Item -Force c:\root\otherproject\myxml.xml $_ }
If you love short syntax:
gci c:\root\output\*\bin\x64\release | % { cp -Force c:\root\otherproject\myxml.xml $_ }
I have an issue when trying to bulk rename files within folders using forfiles and Powershell. The issue I am having is I need to search for files with _T_ in them, so I use the search notation *_T_*.cr2 as they are all RAW files. In a given folder there will be 143 or so RAW files, 69 of them will have _T_ in their names and the rest don't. What I want to achieve is run a quick command that weeds out all files with _T_ in them and then adds an extra _ before the file.
So before: Ab01_T_gh.cr2 and after _Ab01_T_gh.cr2
My issue is that searching for the _T_ files causes the command to keep executing over and over and over, so the file eventually looks like _____________Ab01etc until it hits the Windows file name limit.
Here's what my forfiles command looks like:
forfiles /S /M *_T_*.cr2 /C "cmd /c rename #file _#file"
It works but it works a little too well.
I also tried Powershell with the same result.
Get-ChildItem -Filter "*_T_*.cr2" -Recurse | Rename-Item -NewName { "_" + $_.Name}
Perhaps there's a way I could split up the "find" and "rename" parts of the code? Any help would be greatly appreciated! I can't manually separate out the _T_ files as it would be very time intensive as each parent folder will sometimes have 75 subfolders with 143 RAW files in each.
This works fine:
#echo off
setlocal EnableDelayedExpansion
for %%a in (*_T_.cr2) do (
set z=%%a
set z=!z:~,1!
if not !z!==_ rename %%a _%%a
)
I had to drop forfiles since I could not work with the # variables (maybe with findstr).
I resorted to a simple for loop on the pattern, and only rename if not already starts by underscore.
Recursive version:
#echo off
setlocal EnableDelayedExpansion
for /R . %%a in (*_T_.cr2) do (
echo %%~na%%~xa
set z=%%~na
set z=!z:~,1!
if not !z!==_ rename %%a _%%~na%%~xa
)
Here's a PowerShell version based on your original code:
Get-ChildItem -Filter "*_T_*.cr2" -Recurse |
Where-Object { $_.Name.Substring(0,1) -ne '_' } |
Rename-Item -NewName $("_" + $_.Name)
I need to copy all .TXT files under a directory [subfolders included] and copy them to another directory [without subfolders] but I can't change the extension of the files after I copy them, for example to .OLD or .TXT.OLD. Is that possible to do it via .bat file? Is there any special tool to use instead?
Consider using powershell instead:
Get-ChildItem C:\path\to\root\dir -recurse | where {$_.extension -eq ".txt"} | % {Copy-Item $_.FullName -Destination C:\path\to\target\dir}
You just need to use the root part of the filename and then append your own extension in the rename. For example, to demonstrate that (you don't say you're having any issue with the logic of the actual process, just the extension manipulation, so that's what I'm addressing):
FOR %%I IN (*.txt) DO REN %%I %%~nI.txt.old
This will take all *.txt files in the current directory and rename them to the same root + extension .txt.old. There's a possible complication in that the resulting filename cannot already exist in a rename, so you may want to put it into a more complex loop such as:
FOR %%I IN (*.txt) DO (
[do other stuff here]
IF EXIST %%~nI.txt.old DEL %%~nI.txt.old
REN %%I %%~nI.txt.old
)
Try for /? at a command prompt and check out the last part of it for the syntax of filename/path substitution parameters
I need copy credits.jpg from C:\Users\meotimdihia\Desktop\credits.jpg to D:\Software\destinationfolder and all subfolders
I read many and i write
/R "D:\Software\destinationfolder" %%I IN (.) DO COPY "C:\Users\meotimdihia\Desktop\credits.jpg" "%%I\credits.jpg"
then i save file saveall.bat but i run it , it dont work at all.
help me write 1 bat
Give this a try:
for /r "D:\Software\destinationfolder" %i in (.) do #copy "C:\Users\meotimdihia\Desktop\credits.jpg" "%i"
Of course, if it's to go into a batch file, double the '%'.
This was the first search I found on google for batch file copy file to all subfolders.
Here's a way with xcopy.
There's also robocopy but that would be too powerful for a simple task like this. (I've used that for entire drive backups because it can use multi-threading)
But, let us focus on xcopy.
This example is for saving to a file with the extension .bat. Just drop the additional % where there is two if running directly on the command line.
cd "D:\Software\destinationfolder"
for /r /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
cd "D:\Software\destinationfolder" change directory to the folder you want to copy the file to. Wrap this in quotes if the path has whitespaces.
the for loop - See help here. Or type for /? in a command prompt.
/r - Loop through files (recurse subfolders)
/d - Loop through several folders
%%I - %%parameter: A replaceable parameter
xcopy - Type xcopy /? in the command line for lots of help. You may need to press Enter to read the entire help on this.
C:\temp\file.ext - The file you want to copy
"%%~fsI" - Expands %%I to a full pathname with short names only
/H - Copies files with hidden and system file attributes. By default, xcopy does not copy hidden or system files
/K - Copies files and retains the read-only attribute on Destination files if present on the Source files. By default, xcopy removes the read-only attribute.
The last two parameters are just examples if you're having trouble with any read-only files and will retain the most important file properties.
Lots more xcopy parameters here
xcopy examples here
Just for completeness. This example below will copy the same file in each folder of the current directory and not any sub-folders. Just the /r option is removed for it to behave like this.
for /d %%I in (*) do xcopy "C:\temp\file.ext" "%%~fsI" /H /K
If you can use it: Here is a PowerShell solution (PowerShell is integrated in Windows 7 and available from XP and up):
$file = "C:\...\yourfile.txt"
$dir = "C:\...\YourFolder"
#Store in sub directories
dir $dir -recurse | % {copy $file -destination $_.FullName}
#Store in the directory
copy $file -destination $dir
I'm pretty sure that the last line can be integrated in dir ... but I'm not sure how (I do not use PowerShell very often).