I'm trying to write a batch script to list all the folders, sub-folders and files inside a directory, and then output everything to a .csv-file. I have tried this using the tree command but I also need the "Creation date" and "Last modified" date to be included. The tree command doesn't seem to support that. Is there any other way to do it?
Example:
tree "C:\Windows" /A /F > "C:\Log.csv"
Use a PowerShell one liner:
Get-ChildItem -Recurse | Select-Object FullName,CreationTime,LastWriteTime|Export-Csv C:\log.csv -NotypeInformation
if necessary wrapped in a batch:
powershell -NoP -C "Get-ChildItem -Recurse | Select-Object FullName,CreationTime,LastWriteTime|Export-Csv C:\log.csv -NotypeInformation"
What about Dir /S *.*?
The /S stands for "go through the directory and all subdirectories".
Sorry. I missed the part where you needed the creation dates. Those ones can be achieved as mentioned in this post.
Related
I have a bunch of sql files that I would like to rename as they have the schema name at the start of the file like the following:
SCHEMA.TABLE.sql
Which I want to be renamed to:
TABLE.sql
Unfortunately as I am trying to do this on my work laptop, I can't download any third party software such Bulk Rename Utility so I am trying to do this using CMD.
What I attempted was the following:
REN SCHEMA.*.sql *.sql
which looks like it finds a match but nothing changes. So not sure what I need to change so would appreciate any advice as to where I am going wrong.
The ren command interprets the * in the target pattern *.sql so that it matches all source characters up to the last ., meaning that it matches the portion SCHEMA.TABLE of your sample. Therefore, the target name equals the source name.
Consult this great post on Super User: How does the Windows RENAME command interpret wildcards?
To remove the SCHEMA. part from your file names you need to use a for /F loop:
for /F "eol=| tokens=1* delims=." %I in ('dir /B /A:-D-H-S "*.*.sql"') do ren "%I.%J" "%J"
This fails in case there are more than one ., like in SCHEMA..TABLE.sql, for example.
Do not forget to double the %-signs when you want to use that code in a batch-file!
1.This can be done using powershell script pretty easily. Copy following script, edit $path to the path where the files are located.
$path = 'D:\MyFolder\My docs\testdelete'
$pathObject = get-childitem -path $path | where-object {$_.Psiscontainer -eq $false}
foreach($file in $pathObject)
{
$fileNameArray = $file.Name.Split(".")
Rename-Item -Path $file.FullName -NewName ($fileNameArray[1]+".sql")
}
2.Next save this script in a file named, say testrename.ps1. Open cmd change directory to the location where you saved testren.ps1 file and further run following command:
powershell -file "testren.ps1"
Here $path is the path where the files are located. $pathObject will get all the files in the said location. Further we iterate each file, split the name to get what comes after 'schema' and further rename it. If cmd is not a restriction then 1st script can also be directly run by opening powershell window on windows, copy paste the script after modifying the path and hit Enter key. In this case step 2 is not required.
This question kind of look like this one here
Windows-cmd-add-suffix-to-all-files-in-folder, but it has some additional elements I'm not able to overcome.
I have a main folder, containing many subfolders, with a lot of .xlsb files, and I wish to add a postfix " 2018" to eaxh file, e.g. after renaming a file with name "abc.xlsb" will be called "abc 2018.xlsb". I'm having problem with finding a code, making a script in the command line that will do this for all subfolders.
For now, this is what I have for one folder:
rename *.xlsb "* 2018.xlsb"
,but the resulting name for, e.g., "a.xlsb" comes out "a.xlsb 2018.xlsb".
Also, how do I do this for all folders?
EDIT: I found a command to rename for all subfolders:
for /r %x in (*.xlsb) do ren "%x" *.xlsb
,but
for /r %x in (*.xlsb) do ren "%x" "* 2018.xlsb"
gives, e.g. for a file "abc.xlsb" --> "abc.xlsb 2018.xlsb"
This is pretty straightforward in PowerShell. Use the BaseName and Extension members separately. When you are confident that the files will be renamed correctly, remove the -WhatIf from the Rename-Item cmdlet.
Get-ChildItem -File -Recurse -Filter '*.xlsd' |
ForEach-Object {
$newname = $_.BaseName + ' 2018' + $_.Extension
Rename-Item -Path $_.FullName -NewName $newname -WhatIf
}
If you must run this from a cmd.exe shell, then put the PowerShell code above into a filename such as renappend.ps1. Then run it as shown.
powershell -NoLogo -NoProfile -File renappend.ps1
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)
Today I walked into my office and a couple guys were renaming hundreds and hundreds of files manually on the computer. I looked up ways to rename multiple files. I currently know how to replace a value with another value. Say for example Dir | Rename-Item –NewName { $_.name –replace " ","_" } which replaces all spaces in a file name to an underscore.
However, for my particular task, I need to add just a word in front of all of the file names, for example:
Johnhancock.pdf, AnitaMann.pdf, AmandaHugginkiss.pdf
into
(scores)Johnhancock.pdf, (scores)AnitaMann.pdf, (scores)AmandaHugginkiss.pdf
How would I would I do this? The parentheses are needed for this particular project.
In that scriptblock you just have to generate the name that you want for each item. $_ represents the current item. For your example it would be:
Get-ChildItem | Rename-Item -NewName { "(scores)$($_.Name)" }
This will process an entire directory tree with a batch file. Remove the echo if what you see on the screen matches what you want. Remove the /r to process only the current folder.
#echo off
for /r %%a in (*.txt) do echo ren "%%a" "(scores)%%~nxa"
What worked for me is the code as follows (I could not get the above to work):
Get-ChildItem *.pdf | rename-item -NewName { "(score)" + $_.Name }