Copying a File to Multiple Subdirectories in Same Directory - windows

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 $_ }

Related

How can I use a bat file to mass-copy files from child-dir to root?

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.

CMD - Copy null to multiple files

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

Remove everything in the path except some files in Windows CMD

When it comes to Windows command prompt, I always get confused about path, which seems so easy to grasp in Linux Terminal counterparts.
What I wanna do is, I want to delete everything(both files and folders in one command) in the path (for eg. pub\static directory path) except a file called .htaccess relative from my root directory(C:\wwwroot\Magento2), which is open in Windows command prompt.
How can I do this, I tried the below for loop, but it deletes .htaccess instead. Also how to avoid for loop altogether but get the job done as I described (relative from root directory) ?
for %i in (C:\wwwroot\Magento2\pub\static\*) do if not %i == .htaccess del %i
So I googled a bit and tried below one, but it doesn't do anything:
FOR /D %p IN ("C:\wwwroot\Magento2\pub\static\*.*") DO ( IF NOT %p == .htaccess ( IF EXIST %~sp\NUL ( RMDIR "%p" /S /Q ) ELSE ( DEL /F "%p" ) ) )
Single PowerShell command:
Get-ChildItem C:\inetpub\wwwroot\Magento2\pub\static -Recurse |
Where-Object { $_.Name -ne ".htaccess" } | Remove-Item -Recurse

Batch renaming with forfiles, or potentially Powershell

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)

.Bat file to copy txt files without changing extension?

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

Resources