How to save file structure to text file? - powershell

I have a folder of media files that are about 1TB big. I want to save the file names and directory structure to a text file for backup and reference. I want to attach a batch or PowerShell script to my backup process so the file gets saved before the backup. Does anyone know an easy way to do this?

You can use the built in tree.com utility:
tree c:\folder /F
There's also a PowerShell function, Show-Tree, in PSCX 2.0:
http://rkeithhill.wordpress.com/2010/05/10/pscx-2-0-show-tree/

a pure dir solution
dir /b /s c:\folder >foldertree.txt
has the advantages over Shay and mjolinor solutions that
it does not require powershell, just a plain cmd command
the result list contains the fully specified filenames which is a better format for postprocessing of any kind.

To just save the directory structure and file names:
get-childitem <dir> -recurse | select -expand fullname > dirtree.txt

Open powershell in the folder
then run this to show the files tree in your powershell:
tree /a /f
or save it as txt file:
tree /a /f > tree.txt

Related

Get path for all files in folder exclude 1 file. Command Line

I am trying get path all files and folder in indicated path and except folder Sql as a list. Only folders in the indicated location, no subfolders and their contents.
I am trying this:
powershell Get-ChildItem bin\Release\* -Exclude Sql -Name
But it return only name without path. I need bin\Release\ + Name or full path.
I was trying to:
for /f "usebackq tokens=*" %a in (`dir /b/n "bin\Release\*"`) do echo bin\Release\%~a
But don't know how change echo to list, and how remove folder Sql.
I will be grateful for your help, I cannot cope with it myself.

copy "this folder" to network drive (Robocopy or PowerShell)

Have a folder structure on local drive like this
and a similar structure on network drive like
C:\Documents\Projects\
\\myServer\Storage\Projects\
For each new project I create locally folders like
\Projects\abc\Subfolder1
\Projects\abc\Subfolder2
I need PowerShell or Batch file. The batch is stored in folder abc and it shall perform
Robocopy C:\Documents\Projects\abc \\myServer\Storage\Projects\abc
(of course some switches are reasonable)
Next project, for example xyz
\Projects\abc\Subfolder1
\Projects\abc\Subfolder2
I expect
Robocopy C:\Documents\Projects\xyz \\myServer\Storage\Projects\xyz
Each time I create new project I copy my sample folder structure that is supposed to contain the batch so I can quickly do copy for the project to network,
I think this can be solved by using variables for local and server base path or by doing som
I don't know which Robocopy parameters you need, but this could be a start. This assumes that you place this script in each \Projects\abc directory and run it from there.
$DestinationShare = '\\myServer\Storage'
$SourcePath = $PSScriptRoot
$DestinationPath = $PSScriptRoot -replace ".*?(?<path>\\Projects\\.*)","$DestinationShare`${path}"
robocopy $SourcePath $DestinationPath
This could be turned into a function that would have less static coding.
It is going to be simpler to just robocopy the entire Projects directory whenever it is required.
In batch you could do something like this:
#echo off
set "src=%~dp0"
set "dst=\\myServer\Storage"
for /d %%d in (%src%.) do set "name=%%~nd"
robocopy %src% %dst%\%name%\ /mir
%~dp0 is the directory in which the script resides. The for loop then extracts the name of that directory from the full path.

Check if a file with a pattern is available in the directory using batch script

A file is dropped in a directory when another job finished processing it.
The requirement is to run a batch script to check whether the file is available for today. and if available, I need to execute certain batch script. (Example: File with Naming pattern ABC-D*.txt should be available with modification date=today)
What I have figured out till now is:
FOR /F "tokens=1,2,3* Delims=/ " %%I in ('DATE /T') DO set TODAY=%%I-%%J-%%K
xcopy C:\BatchJobs\Odd*.* /L /D:%TODAY%
Running this is giving the output:
C:\BatchJobs\OddEven.txt
File cannot be copied onto itself
0 File(s)
C:\BatchJobs\OddEven.txt, showing in console is what I need. But I need to store it in some file or in some variable to be able to use this path later in my batch script. Can somebody help me in storing this file path in a variable or a file? Or suggest some other ways to achieve this goal?
You could use forfiles which is capable of filtering files by their last modification date, but not regarding the time:
forfiles /P "D:\ROOT" /M "ABC-D*.txt" /D +0 /C "cmd /C echo #file"
Instead of echo you can state your batch script to execute.
This code will identify "ABC-D*.txt" files last written today. Place the code below into a file such as doit.ps1 (but choose a better name). I did an attrib command on the file, but you will want to do something else.
$srcdir = 'C:\src\t\empty'
Get-ChildItem -File -Path $srcdir -Filter 'ABC-D*.txt' |
Where-Object { $_.LastWriteTime -ge [datetime]::Today } |
ForEach-Object {
# Do something to the $_ file
& attrib $_
}
Then, you can run it from a cmd shell with:
powershell -NoProfile -File doit.ps1

Robocopy copy contents of current folder

How would you translate this xcopy command into Robocopy:
xcopy *.* "C:\DestinationFolder\"
Keeping in mind that the current folder where the command is run changes dynamically (and hence the source folder is unknown in advance).
Thanks.
robocopy . "c:\dest"
Note you don't need to specify a wildcard in robocopy, by default it copies everything unless you use the /xf /xd flags to exclude certain files.
Robocopy DOES support wildcards.
You're expecting > robocopy SOURCE DEST but type > robocopy *.txt c:\folderdest\ to copy the current folder. If you look at the output from robocopy it will show "Files : *.txt" and "Source = c:\folderdest"
So in fact you can do > robocopy WILDCARD SOURCE DEST. If you want to use the CURRENT folder you need to use . (as has been mentioned here). So you would use > robocopy *.txt . c:\folderdest\.
Screenshot: http://i.stack.imgur.com/Xyxt4.png
As an addition:
If robocopy is started from an administrator console, the current folder "." will point to Windows\system32.
You can use the following commands at the top of your batch file to fix this:
#setlocal enableextensions
#cd /d "%~dp0"

How to copy files from folder tree dropping all the folders with Robocopy?

I have the following folder structure:
FolderA
--Folder1
--Folder2
--Folder3
...
--Folder99
Folders 1 through 99 have files in them.
All I want to do is to copy ALL THE FILES into ONE FOLDER, basically do a FolderA copy, and wipe out Folders 1-99 keeping all the files.
I'd like to do it with Robocopy from cmd.exe if possible (Windows Server 2008)
Why use robocopy? It's a good tool for a specific task but this is not the one.
You can simply use what cmd already gives you:
for /r %f in (*) do #copy "%f" target
This will essentially "flatten" your directory hierarchy. for /r will walk a directory tree recursively, looking for file names matching the given pattern. You can also specify the directory to start in:
for /r FolderA %f in (*) do #copy "%f" target
Within the loop it's just a simply copy of the file into a specified folder.
Robocopy is a great tool... when you have a job it can handle. Why not use xcopy?
If you have two drives you can just use xcopy:
XCOPY C:\*.* D:\NewFolder\ /S
Or use XXCOPY for one drive:
XXCOPY C:\*.* C:\NewFolder\ /S /CCY
XXCOPY
Get-ChildItem -Path source -Recurse -File | Move-Item -Destination dest