使用文件名和当前日期戳创建多个txt(Text_01.txt --- Text_50.txt)文件

时间:2018-11-19 20:06:44

标签: windows powershell

有人可以帮我在Powershell中编写脚本来 创建带有日期和每个文件中文件名的50个.txt文件? 然后重命名它们并将新名称和日期添加到第二行?

3 个答案:

答案 0 :(得分:1)

IMO,您在强调人们回答的诚意,
还没有任何投票。

#pragma once

#include <vulkan/vulkan.h>
#include <iostream>
#include <glm/glm.hpp>

#include "Pipeline_Wrapper.h"
#include "GLFW_Wrapper.h"
#include "Extensions_Manager.h"
#include "Swapchain_Wrapper.h"
#include "Queue_Wrapper.h"
#include "Commands_Wrapper.h"
#include "Mesh.h"
#include "Texture.h"
#include "Model.h"

typedef struct
{
    glm::mat4 model;
    glm::mat4 view;
    glm::mat4 proj;
}UniformBufferObject;

class Vulkan_Graphics
{
private:
    VkInstance                      vkInstance;

    Command                         *graphicsCommandPool;

    VkDebugUtilsMessengerEXT        callback;
    VkSurfaceKHR                    surface;

    uint32_t                        deviceCount;
    VkPhysicalDevice                *devices;
    bool                            logicalDeviceCreated;

    VkQueue                         graphicsQueue;
    VkQueue                         presentQueue;
    VkQueue                         transferQueue;

    VkDeviceQueueCreateInfo         *queueCreateInfo;
    VkPhysicalDeviceFeatures        deviceFeatures;

    VkSemaphore                     imageAvailableSemaphore;
    VkSemaphore                     renderFinishedSemaphore;

    std::vector<VkLayerProperties>  validationAvailableLayers;
    std::vector<const char*>        validationInstanceLayerNames;
    std::vector<const char*>        validationDeviceLayerNames;

    std::vector<VkBuffer>           uniformBuffers;
    std::vector<VkDeviceMemory>     uniformBuffersMemory;
    uint32_t                        uniformBufferCount;

   void CreateVulkanInstance();
    void CreateLogicalDevice();

    void CreateSemaphores();

    void CreateUniformBuffer();

    void SetupDebugCallback();

    bool CheckValidationLayerSupport();

    void PickPhysicalDevice();

    VkDeviceCreateInfo GetDeviceInfo(bool validation);

    VkPhysicalDevice GetPhysicalDevice(){ return physicalDevice; }

    bool IsDeviceSuitable(VkPhysicalDevice device);

public:
   GLFW_Wrapper                 *glfwWrapper;
    Commands_Wrapper                *cmdWrapper;
    Extensions_Manager              *extManager;
    Queue_Wrapper                   *queueWrapper;
    Swapchain_Wrapper               *swapchainWrapper;
    Pipeline_Wrapper                *pipeWrapper;

    UniformBufferObject             ubo;

    Pipeline                        *currentPipe;

    VkPhysicalDevice                physicalDevice;
    VkDevice                        logicalDevice;

    Vulkan_Graphics(GLFW_Wrapper *glfwWrapper, bool enableValidation);
    ~Vulkan_Graphics();

    Command* GetGraphicsPool(){ return graphicsCommandPool; }
    Pipeline* GetCurrentPipe(){ return currentPipe; }
    VkQueue GetGraphicsQueue(){ return graphicsQueue; }

    VkFramebuffer VRenderBegin();
    void VRenderEnd();

    uint32_t BeginDrawFrame();
    void EndDrawFrame(uint32_t imageIndex);
    Command* GetGraphicsCommandPool(){ return graphicsCommandPool; }

    VkBuffer GetUniformBufferByIndex(uint32_t index);

    static int CreateBuffer(VkDeviceSize size, VkBufferUsageFlags usage, 
VkMemoryPropertyFlags properties, VkBuffer * buffer, VkDeviceMemory * 
bufferMemory);
    static uint32_t FindMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags 
properties, VkPhysicalDevice physicalDevice);
    static VkImageView CreateImageView(VkImage image, VkFormat format, VkDevice 
logDevice);
    static void CopyBuffer(VkBuffer srcBuffer, VkBuffer dstBuffer, 
VkDeviceSize size, VkDevice lDevice, VkPhysicalDevice physDevice);

};

extern Vulkan_Graphics vGraphics;

答案 1 :(得分:0)

一个班轮:

1..50 | ForEach-Object {New-Item  -Path "C:\temp\$($_.ToString("00"))_$(Get-Date -Format 'yy-mm-dd').txt"} 

或者如果您想向文件中写入内容,并使其更具可读性

foreach ($N in 1..50){
    $FilePath = "C:\temp"
    $FileName = "Text_$($N.ToString("00")).txt"
    New-Item -Path "$FilePath\$FileName"
    $Date = Get-Date 
    Add-Content -Path "$FilePath\$FileName" -Value $FileName
    Add-Content -Path "$FilePath\$FileName" -Value $Date.ToString("hh:mm:ss dd/MM/yy")
}

请注意,您将需要弄弄$ Date.ToString()位以允许重复的区别。

答案 2 :(得分:0)

这是另一种方法。它会将大多数$ Vars分解为单独的项,以使过程更加清晰... [咧嘴]

$DestDir = $env:TEMP
$FNamePrefix = 'Text_'
$FNameExt = '.txt'

# if you want the file content to be all on one line, remove this $Var from the "Set-Content" line
$NewLine = [environment]::NewLine

$FileCount = 5
foreach ($Count in 1..$FileCount)
    {
    $TimeStamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
    # create a left-padded 2 digit number string
    $Number = '{0:D2}' -f $Count
    $FileName = -join ($FNamePrefix, $Number, $FNameExt)
    $FullFileName = Join-Path -Path $DestDir -ChildPath $FileName

    # this will create the file or overwrite any pre-existing one
    #    the "-Value" stuff will be in the file
    #    to put the info all on one line use the following for the "-Value" info
    #    ""$TimeStamp; $FileName"
    Set-Content -LiteralPath $FullFileName -Value "$TimeStamp$NewLine$FileName" -Force
    }

没有屏幕输出。第一个文件的内容是...

2018-11-19 15:00:51
Text_01.txt

该文件的名称为Text_01.txt。 [咧嘴]