Michael Wells
Michael Wells

Reputation: 41

How do I make a folder with today's date and then copy a file to it?

I am trying to automate my day, part of which is creating a daily work folder and copying a 'Job Notes' file which I use, rename and save per job, repeat again the next day.

I found a script to create a folder based on today's date, which works, and I've obviously found examples of how to copy a file. What I'm looking for is to combine the two. I just need something basic. Here is the code I have for the folder creation. I just don't know what to add to the "copy file" action to make it use that particular directory name (based on today's date).

$location = New-Item -Path C:\Users\XXXXXXX\Documents\test -ItemType Directory -Name ("$(Get-Date -f MM_dd_yy)")

$dirname = "$((get-date).toString('MM-dd'))"
md $dirname

Any help would be greatly appreciated, including if the above code is not the easiest way to accomplish this.

P.S. Whether a batch file or PowerShell for doing this would be fine, I am playing around learning both methods.

Upvotes: 2

Views: 11114

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

You already have the location in your $location variable. Simply use that as the destination for Copy-Item:

$basedir = "$env:USERPROFILE\Documents\test"
$today   = (Get-Date).ToString('MM_dd_yy')

$location = New-Item -Path $basedir -Type Directory -Name $today

Copy-Item 'C:\path\to\job_notes.txt' -Destination $location

md is just a shorthand (an alias for a function that calls New-Item with the required parameters).

Upvotes: 7

Dennis van Gils
Dennis van Gils

Reputation: 3452

Try this:

@echo off
set "location=C:\Users\XXXXXXX\Documents\test"
md "%date%"
copy "%location" "%date%"

Upvotes: 0

Related Questions